Asp.net MVC 5重定向到帐户/登录 [英] Asp.net MVC 5 redirect to Account/Login

查看:224
本文介绍了Asp.net MVC 5重定向到帐户/登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm learning ASP.NET MVC. I have MVC version 5.2.2.0

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

I attached Authorize attribute to an action method Index() in Employee controller.

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

In the Web.config file, I changed authentication tag data as follows:

<system.web>
    <authentication mode="Forms">
        <forms loginurl="~/Authentication/Login"></forms>
    </authentication>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
</system.web>

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

What is expected is that when localhost:port/Employee/Index is accessed, the user should be redirected to localhost:port/Authentication/Login

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

But it is redirecting to localhost:port/Account/Login

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

By looking at other links, I tried the following things:

1.添加

<add key="autoFormsAuthentication" value="false" />
<add key="enableSimpleMembership" value="false"/>
<add key="loginUrl" value="~/Authentication/Login" />
<add key="PreserveLoginUrl" value="true" />

访问Web.config的appSettings部分

to appSettings section of Web.config

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

2.Changed IIS 8 Anonymous Authentication from Specific User to Application Pool Identity

3.当以上两个都不起作用时,我将身份验证标签更改为

3.When the above two didn't work, I changed authentication tag to

<authentication mode="Windows" />

但没有一个.

编辑 不要做我上面提到的事情1、2、3.只需做答案中提到的更改

EDIT Don't do the things 1, 2, 3 I mentioned above. Just do the changes mentioned in the answer

推荐答案

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

The problem is that you will have the OWIN middleware configured by default to redirect to "/Account/Login" for cookie authentication.

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

Open /AppStart/Startup.Auth.cs and edit the following block of code to target our own 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);

这篇关于Asp.net MVC 5重定向到帐户/登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆