Asp.net mvc Asp.net MVC 5重定向到帐户/登录
我正在学习ASP.NETMVC。我有MVC版本5.2.2.0 我将Authorize属性附加到Employee controller中的操作方法Index() 在Web.config文件中,我更改了身份验证标记数据,如下所示:Asp.net mvc Asp.net MVC 5重定向到帐户/登录,asp.net-mvc,iis,asp.net-mvc-5,Asp.net Mvc,Iis,Asp.net Mvc 5,我正在学习ASP.NETMVC。我有MVC版本5.2.2.0 我将Authorize属性附加到Employee controller中的操作方法Index() 在Web.config文件中,我更改了身份验证标记数据,如下所示: <system.web> <authentication mode="Forms"> <forms loginurl="~/Authentication/Login"></forms> <
<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
但它正在重定向到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" />
转到Web.config的appSettings部分
2.将IIS 8匿名身份验证从特定用户更改为应用程序池身份
3.当以上两个都不起作用时,我将身份验证标签改为
<authentication mode="Windows" />
但都不管用
编辑
不要做我上面提到的第1、2、3项。只需执行回答中提到的更改问题在于,您将在默认情况下将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);
app.UseCookieAuthentication(新的CookieAuthenticationOptions
{
AuthenticationType=DefaultAuthenticationTypes.ApplicationOkie,
LoginPath=新路径字符串(“/Account/Login”),
Provider=新CookieAuthenticationProvider
{
//允许应用程序在用户登录时验证安全戳。
//这是一种安全功能,在您更改密码或向帐户添加外部登录时使用。
OnValidateIdentity=SecurityStampValidator.OnValidateIdentity(
validateInterval:TimeSpan.FromMinutes(30),
regenerateIdentity:(管理器,用户)=>user.GenerateUserIdentityAsync(管理器))
}
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
也许这已经改变了。我正在学习ASP.NET 5(dnx451和MVC 6.0.0-rc1-final),在那里,您必须为服务中的登录定义默认重定向地址:“ConfigureServices”方法而不是“Configure”方法
public void配置服务(IServiceCollection服务)
{
服务.附加性(配置=>
{
//增加一些要求
configure.User.RequireUniqueEmail=true;
configure.Password.RequiredLength=8;
//如果呼叫必须[自动调整大小],请定义默认页面
configure.Cookies.applicationcokie.LoginPath=“/Auth/Login”;
})
.AddEntityFrameworkStores();//使用实体框架存储用户数据
}
在web.config中注释以下行,然后它将不会自动转到“帐户/登录”。我在VS2015中进行了测试,效果很好
要注释掉的行:
<remove name="FormsAuthentication"/>
您是指Startup.cs还是Startup.Auth.cs?我想是Startup.Auth.cs是的,是Startup.Auth.cs。我已经找了好一阵子了。谢谢
<remove name="FormsAuthentication"/>