MVC 5使用身份验证模式的外部身份验证=表格 [英] MVC 5 External authentication with authentication mode=Forms
问题描述
我正在关注更改为authentication mode="Forms"
,它将停止工作.
I'm following this tutorial to create a simple MVC 5 app with external authentication. It's working fine, but, if I change the authentication mode="None"
to authentication mode="Forms"
it stops working.
我在以下位置上空了
await HttpContext.GetOwinContext().Authentication.GetExternalLoginInfoAsync()
我读了很多有关在重定向时禁止FormsAuthentication的内容.我不知道这是否是正确的路径,但我尝试安装此
I'm reading a lot about something to suppress FormsAuthentication on redirect. I don't know if it's the right path, but I tried to install this nuget packet and the problem is still there.
那么,为什么每次更改身份验证模式时我都为空?
So, why I'm getting null everytime I change the authentication mode?
推荐答案
通过将Response.SuppressFormsAuthenticationRedirect = true
添加到ChallengeResult
类中,我可以使它正常工作(OWIN和FormsAuthentication).
I was able to get this working (OWIN and FormsAuthentication) by adding Response.SuppressFormsAuthenticationRedirect = true
to the ChallengeResult
Class.
如果您正在学习本教程,则代码如下:
If you are following the tutorial, here is the code:
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);
}
}
这篇关于MVC 5使用身份验证模式的外部身份验证=表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!