gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 23:19:37 24 4
gpt4 key购买 nike

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

我将 Authorize 属性附加到 Employee Controller 中的操作方法 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>

预期的是,当访问 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);

关于asp.net-mvc - Asp.net MVC 5 重定向到帐户/登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31219759/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com