asp.net-mvc – MVC 5具有身份验证模式的外部身份验证=表单

我正在关注
this tutorial以创建一个带有外部身份验证的简单MVC 5应用程序.它工作正常,但是,如果我将身份验证模式=“无”更改为身份验证模式=“表单”,它将停止工作.

我搞砸了:

await HttpContext.GetOwinContext().Authentication.GetExternalLoginInfoAsync()

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

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

解决方法

通过将Response.SuppressFormsAuthenticationRedirect = true添加到ChallengeResult类,我能够使这个工作(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);
    }
}

dawei

【声明】:丽水站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。

相关文章