0

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 ?

1
  • 1
    Possible duplicate of THIS. Commented Apr 22, 2015 at 6:19

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

It was very helpful when I was creating custom provider. Thank you @sarmin

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.