简体   繁体   English

如何在C#MVC4中的基于表单的身份验证中添加两个不同的登录URL?

[英]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. 我已经为一般用户部分创建了一个自定义成员资格提供程序,现在我想在web.config文件中进行表单身份验证。 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. 当我使用自定义成员资格提供程序时,我还将配置自定义角色提供程序,然后将以下行添加到我的web.config文件中。 You can see if it supports your scenario. 您可以查看它是否支持您的方案。

Step 1: 第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: 第2步:

Add [Authorize] attribute to Index method of HomeController. [Authorize]属性添加到HomeController的Index方法。

Step 3: 第三步:

under the <appSettings> section: <appSettings>部分下:

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

Step 4: 第四步:

Comment on InitializeSimpleMembership from AccountController and override login action as per requirement. AccountController InitializeSimpleMembership注释,并根据要求覆盖登录操作。

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. 我们无法在webconfig文件中设置用于登录的两个登录URL。如果创建自己的自定义成员资格提供程序,则必须将其设置为defaultprovider,以使[Authorize]属性对其适用。 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 在Web配置表单中启用了身份验证

<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. 因此,当我使用[Authorize]属性时,它带我进入了管理员登录页面,这是预期的。 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. 因此,我创建了一个[AuthorizeUser]属性,该属性现在将用户带到用户登录部分。

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 此属性将我的用户带到〜/ login的用户登录页面

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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