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

问题描述 投票:9回答:2

我正在关注this tutorial创建一个带有外部身份验证的简单MVC 5应用程序。它工作正常,但是,如果我将authentication mode="None"更改为authentication mode="Forms"它会停止工作。

我搞砸了:

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

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

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

asp.net-mvc authentication forms-authentication asp.net-mvc-5 owin
2个回答
19
投票

通过将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);
    }
}

2
投票

通常,您可以设置authentication mode="None",当用户未经过身份验证或者您计划开发自定义身份验证代码时。 MVC 5已更新为使用ASP.NET标识进行身份验证。

ASP.NET Identity支持基于声明的身份验证,其中用户的身份表示为一组声明。在这里,你设置authentication mode="Forms",声明不起作用,因为ASP.NET Forms Authentication不支持声明。这就是你获得空值的原因。

© www.soinside.com 2019 - 2024. All rights reserved.