使用ASP.NET Identity遇到令人沮丧的问题。该应用不会重定向到LoginPath
,而是提供401。
我已将AuthorizeAttribute
过滤器添加到我的过滤器,将AllowAnonymous
属性添加到“登录”操作。
我已将Configuration()
中的Startup.cs
方法重构为:
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
}
public void ConfigureAuth(IAppBuilder app)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = "ApplicationCookie",
LoginPath = new PathString("/user/login")
});
}
根据这个问题: MVC 5 Redirect to Login Page Not Working with OWIN
我编辑了web.config文件,并添加了以下行:
<system.webServer>
<modules>
<remove name="FormsAuthentication" />
</system.webServer>
根据这个问题:ASP.NET MVC 5 Web.config: "FormsAuthenticationModule" or "FormsAuthentication"
我仍然没有得到重定向。另一方面,我不知道这些优秀的绅士们在说什么:https://github.com/aspnet/Security/issues/607
显然这一行应该解决我的问题:
app.UseCookieAuthentication(options => {
options.AutomaticAuthenticate = true;
options.AutomaticChallenge = true;
options.LoginPath = "/Account/Login";
});
包含UseCookieAuthentication
覆盖的包接受lambdas或AutomaticAuthenticate
和AutomaticChallenge
选项,我认为是Microsof.AspNet.Authentication
和Microsof.AspNet.Authentication.Cookies
,但两者都是NuGet失踪。
web.config包含owin密钥:<add key="owin:AppStartup" value="SmartKG.App_Start.Startup" />
UserController
之前,我被重定向到正确的路线,但当然得到了丢失的资源错误。一旦我添加了控制器和操作,重定向问题就开始发生了。