Asp.net MVC 5重定向到帐户/登录

Eng*_*eer 3 iis asp.net-mvc asp.net-mvc-5

我正在学习ASP.NET MVC.我有MVC版本5.2.2.0

我将Authorize属性附加到Employee控制器中的操作方法Index().

在Web.config文件中,我更改了身份验证标记数据,如下所示:

<system.web>
    <authentication mode="Forms">
        <forms loginurl="~/Authentication/Login"></forms>
    </authentication>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
</system.web>
Run Code Online (Sandbox Code Playgroud)

可以预期的是,当访问localhost:port/Employee/Index时,应该将用户重定向到localhost:port/Authentication/Login

但它重定向到localhost:port/Account/Login

通过查看其他链接,我尝试了以下事项:

1.添加

<add key="autoFormsAuthentication" value="false" />
<add key="enableSimpleMembership" value="false"/>
<add key="loginUrl" value="~/Authentication/Login" />
<add key="PreserveLoginUrl" value="true" />
Run Code Online (Sandbox Code Playgroud)

到Web.config的appSettings部分

2.将IIS 8匿名身份验证从特定用户更改为应用程序池身份

3.当上述两个不起作用时,我将认证标签更改为

<authentication mode="Windows" />
Run Code Online (Sandbox Code Playgroud)

但都没有效果.

编辑 不要做我上面提到的第1,2,3项.只需做出答案中提到的更改

Der*_*rek 6

问题是您将默认配置OWIN中间件重定向到"/ Account/Login"以进行cookie身份验证.

打开/AppStart/Startup.Auth.cs并编辑以下代码块以定位我们自己的URL: -

app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LoginPath = new PathString("/Account/Login"),
                Provider = new CookieAuthenticationProvider
                {
                    // Enables the application to validate the security stamp when the user logs in.
                    // This is a security feature which is used when you change a password or add an external login to your account.  
                    OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                        validateInterval: TimeSpan.FromMinutes(30),
                        regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
                }
            });            
            app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
Run Code Online (Sandbox Code Playgroud)