简体   繁体   中英

How to add two different login url in form based authentication in C# MVC4?

I have one login page for admin, another login page for general user. I have created a custom membership provider for general user section, now I want to give form authentication in web.config file. How to do that ?

When i work with custom membership provider, i also configure custom role provider and then add the following lines into my web.config file. You can see if it supports your scenario.

Step 1:

 <authentication mode="Forms">
   <forms loginUrl="~/Account/Login" timeout="2880" />
 </authentication>

<membership defaultProvider="YourCustomMembershipProviderName">
   <providers>
      <clear/>
      <add name="YourCustomMembershipProviderName" type="Logger.SampleApp.Security.Infrustructure.CustomeMembershipProvider" connectionStringName="YourConnectionStringName" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" applicationName="Logger.SampleApp.Client.Web"/>
   </providers>
</membership>

<roleManager enabled="true" defaultProvider="YourRoleProvider"
    <providers>
    <clear/>
      <add name="YourRoleProvider" type="Logger.SampleApp.Security.Infrustructure.CustomRoleProvider" />" 
    </providers>
</roleManager>

Step 2:

Add [Authorize] attribute to Index method of HomeController.

Step 3:

under the <appSettings> section:

<add key= "enableSimpleMembership" value= "false"/>
<add key= "autoFormsAuthentication" value= "false"/>

Step 4:

Comment on InitializeSimpleMembership from AccountController and override login action as per requirement.

We can not set two login urls for login inside webconfig file.If we create our own custom Membership provider, we have to set it as defaultprovider for making the [Authorize] attribute workable for it. But in my case, there were two providers. Both are custom providers, and I wasn't allowed to change the default provider. One provider is used for Admin login(the default provider), another provider used for user login (custom provider). In web config form authentication was enabled

<authentication mode="Forms">
  <forms loginUrl="~/Account/Login" timeout="2880" />
</authentication>

So, when I was using [Authorize] attribute, it was taking me to the admin login page and it is expected. But I needed an attribute which would take me to user login page. So I created a [AuthorizeUser] attribute which is now taking users to user login section.

public class AuthorizeUserAttribute : AuthorizeAttribute
{

    protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
    {
        var username = filterContext.HttpContext.User.Identity.Name;
        if (username != "")
        {
            base.HandleUnauthorizedRequest(filterContext);
        }
        else
        {
            filterContext.Result = new RedirectToRouteResult(new
            RouteValueDictionary(new { controller = "Login", action = "Index" }));
        }
    }
}

This attribute is taking my users into user login page at ~/login

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM