使用Owin身份验证;没有重定向到LoginPath

时间:2016-04-20 14:10:37

标签: c# asp.net asp.net-mvc asp.net-identity owin

使用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或AutomaticAuthenticateAutomaticChallenge选项,我认为是Microsof.AspNet.AuthenticationMicrosof.AspNet.Authentication.Cookies,但两者都是NuGet失踪。

web.config包含owin密钥:<add key="owin:AppStartup" value="SmartKG.App_Start.Startup" />

编辑:我没有提到的一个奇怪的事情:在我添加包含登录操作的UserController之前,我被重定向到正确的路线,但当然得到了丢失的资源错误。一旦我添加了控制器和操作,重定向问题就开始发生了。

0 个答案:

没有答案