How to use login in different areas on Asp.net mvc

Asked

Viewed 991 times

4

I’m creating an app, where it has more than one Area, which is restricted. (as shown in the image below).

inserir a descrição da imagem aqui

My question is how to login in different areas?

Details:

As users of the Admin area and the System area are distinct, each one is saved in a different table in the database.

Authentication is performed with Forms, and authentication to the Admin area already exists:

<authentication mode="Forms">
      <forms loginUrl="~/Autenticacao/Login" defaultUrl="Admin/Home" timeout="2880" />
    </authentication>
  • 1

    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

  • 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?

  • 1

    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.

1 answer

2


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>
  • 2

    I think what he wants is not just the redirect. It’s a login procedure by area.

  • 2

    @Ciganomorrisonmendez, as I’m not sure I’ll leave the redirect part, following what you said, I edited my reply and put a link to an article that explains how to do by areas, it is quite complete =)

  • 1

    Exactly, what I am looking for is a login procedure per area, I saw several examples using more than one area, and I was left in the doubt of how to perform the procedure of executing authentication in separate areas (as in the examples I looked for I did not find anything that did this). The explanation that @Taisbevalle got very good.

  • 1

    @Taisbevalle The second part of the answer does not make sense to me.

  • 1

    @jbueno, the second part of the answer is a copy of the excerpts of the article, as it is very large and full of images only put a part, because the article itself is very good and has no way to be "summarized" to make a good answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.