In his Web.config
, withdraw the part defaultUrl="Admin/Home"
.
<authentication mode="Forms">
<forms loginUrl="~/Autenticacao/Login" timeout="2880" />
</authentication>
In action Autenticacao/Login
, you can redirect users to different locations, something like:
public ActionResult Login(String LoginStr, String SenhaStr, bool ManterConectado)
{
// Seu código de login
return RedirectPaginaInicial(usuario);
}
// Função para retornar a página inicial de cada usuário
private RedirectToRouteResult RedirectPaginaInicial(Usuario usuario)
{
if (usuario.Areas == (int)PaginaPrincipal.ADMIN)
return RedirectToAction("Lista", "PaginaAdministrador");
if (usuario.Areas == (int)PaginaPrincipal.SISTEMA)
return RedirectToAction("Lista", "Sistema");
return RedirectToAction("Lista", "Agenda");
}
In that case, PaginaPrincipal
is a enum
:
public enum PaginaPrincipal
{
[Display(ResourceType = typeof(Translate.Enum), Name = "Administrador")]
ADMIN = 1,
[Display(ResourceType = typeof(Translate.Enum), Name = "Sistema")]
SISTEMA = 2,
}
If you don’t just want a redirect, as the Gypsy comment, has this article "How to Use Areas in ASP.NET MVC 5?", explaining the step by step procedure to use the login by areas, is quite complete.
To make the answer more complete, I removed these excerpts from the article. You will have to include a new @Html.ActionLink
<li>@Html.ActionLink("ADMIN", "Index", "Home", new { area = "Admin" }, null)</li>
<li>@Html.ActionLink("SISTEMA", "Index", "Home", new { area = "Sistema" }, null)</li>
And insert the attribute [Authorize]
above the name of HomeController
:
[Authorize]
public class HomeController : Controller
{
// GET: Admin/Home
public ActionResult Index()
{
return View();
}
}
Open the page _Layout.cshtml
in Areas/Admin/Views/Shared
and add the lines of div
that shows the name of the logged in user, just below the menu "Application name"
.
@Html.ActionLink("Application name", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
<div class="text-info">
@User.Identity.Name
</div>
I think you should have created different projects. That way, (I think) the most you can do is create different Ttributes and validate for them. If you’re using Owin, can be a little different, but I can’t say
– Jéf Bueno
It crossed my mind to create different projects, but I chose to use areas. If I created a different project, maybe you have the opportunity to answer a question, it would be simple also to restrict the entire project, just marking the classes as [Authorize], even if it has no areas?
– Erico Souza
Look, the only way I see of doing this without creating filters
Authorize
different is creating two projects. What you have in the answer does not answer your question, it just redirects to different login pages. About your doubt: yes, it is completely possible, you will do the same thing, as in the original project, but changing the authentication settings.– Jéf Bueno