MVC 5具有身份验证模式的外部身份验证=表单

Fel*_*sso 10 authentication asp.net-mvc forms-authentication owin asp.net-mvc-5

我正在按照本教程创建一个带有外部身份验证的简单MVC 5应用程序.它的工作很好,但是,如果我改变authentication mode="None"authentication mode="Forms"它停止工作.

我搞砸了:

await HttpContext.GetOwinContext().Authentication.GetExternalLoginInfoAsync()
Run Code Online (Sandbox Code Playgroud)

我正在阅读很多关于在重定向上禁止FormsAuthentication的内容.我不知道这是不是正确的路径,但我试图安装这个nuget数据包,问题仍然存在.

那么,为什么每次更改身份验证模式时我都会变为空?

Fel*_*sso 20

通过添加Response.SuppressFormsAuthenticationRedirect = trueChallengeResultClass,我能够使这个工作(OWIN和FormsAuthentication).

如果您正在学习本教程,那么代码如下:

public class ChallengeResult : HttpUnauthorizedResult
{
    public ChallengeResult(string provider, string redirectUri)
        : this(provider, redirectUri, null)
    {
    }

    public ChallengeResult(string provider, string redirectUri, string userId)
    {
        LoginProvider = provider;
        RedirectUri = redirectUri;
        UserId = userId;
    }

    public string LoginProvider { get; set; }
    public string RedirectUri { get; set; }
    public string UserId { get; set; }

    public override void ExecuteResult(ControllerContext context)
    {
        // this line did the trick
        context.RequestContext.HttpContext.Response.SuppressFormsAuthenticationRedirect = true;

        var properties = new AuthenticationProperties() { RedirectUri = RedirectUri };
        if (UserId != null)
        {
            properties.Dictionary[XsrfKey] = UserId;
        }

        context.HttpContext.GetOwinContext().Authentication.Challenge(properties, LoginProvider);
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 谢谢谢谢!我长时间不知疲倦地对着这个问题.如果ExternalLogin方法是AllowAnonymous,我无法理解为什么它被重定向到登录表单! (2认同)