我希望有人可以帮我解决这个问题 - 这让我很生气!:)
我正在尝试使用tinysnake的QQ Connect提供商通过QQ Connect(OAuth 2.0)进行外部登录:https://github.com/tinysnake/microsoft-owin-security-qq
一切似乎都很好 - 我可以通过我的QQ帐户登录,然后通过相应的声明等回复到我的ExternalLoginCallBack方法.我使用这些值通过IAuthenticationManager签署用户 - 一切顺利.但是 - 当我将用户重定向到另一个页面并检查他是否已登录时 - 我从IsAuthenticated值中得到一个错误的值...而我无法读取我之前设置的任何声明.
这可能是一个简单的修复 - 但我现在看不到它:)
一些代码:
AuthConfig:
public static void ConfigureAuthentication(IAppBuilder app)
{
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
// Normal cookie sign in
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
AuthenticationMode = AuthenticationMode.Active
});
// QQ CONNECT
app.UseQQConnectAuthentication(
appId: "XXXXXX",
appSecret: "XXXXXXXXXXXXXXXXX");
}
Run Code Online (Sandbox Code Playgroud)
的AccountController:
//
// POST: /Account/ExternalLogin
[System.Web.Mvc.HttpPost]
[System.Web.Mvc.AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult ExternalLogin(string provider, string returnUrl)
{
// Request a redirect to the …Run Code Online (Sandbox Code Playgroud) 我们有一个基于ASP.NET Mvc 4的遗留系统,现在我们希望通过Azure Active Directory为当前用户和新用户支持Signal Sign On.由于我们已经管理了自己的身份验证工作流,因此ASP.NET身份确实不适合我们的情况.
我已经设法构建了一个演示,该演示正在使用OWIN OpenIdConnect中间件被动模式,而不使用ASP.NET身份.以下代码正常工作:
app.SetDefaultSignInAsAuthenticationType("ExternalCookie");
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = "ExternalCookie",
AuthenticationMode = AuthenticationMode.Passive,
});
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
AuthenticationMode = AuthenticationMode.Passive,
ClientId = ClientId,
Authority = Authority
// More code
});
Run Code Online (Sandbox Code Playgroud)
并在ExternalLoginCallback行动:
public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
{
var authManager = Request.GetOwinContext().Authentication;
var result = await authManager.AuthenticateAsync("ExternalCookie");
authManager.SignOut("ExternalCookie");
//More code to convert to local identity
}
Run Code Online (Sandbox Code Playgroud)
即使使用Google,Facebook或Twitter等其他提供商,这种情况也很常见.我不太清楚的一件事是ExternalCookie,也许我错过了整件事.我的理解是,当外部登录成功时,外部cookie用于存储外部声明身份.然后我们打电话给:
var result = await authManager.AuthenticateAsync("ExternalCookie");
authManager.SignOut("ExternalCookie");
Run Code Online (Sandbox Code Playgroud)
为了获得外部声明身份,然后将外部身份转换为本地身份.我有点困惑为什么SignOut在这种情况下我们必须调用外部cookie.
此外,我不确定在使用外部登录时是否必须使用外部Cookie,或者我们是否有其他方法而不使用外部Cookie.
请有人就这一点作出解释.
asp.net-mvc owin asp.net-mvc-5 azure-active-directory openid-connect
为什么这个示例在使用ApplicationCookie登录之前调用SignOut for ExternalCookie?它只是确保身份验证信息干净的一种方法吗?(完整的例子在这里:http://www.asp.net/identity/overview/getting-started/introduction-to-aspnet-identity)
private async Task SignInAsync(ApplicationUser user, bool isPersistent)
{
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
var identity = await UserManager.CreateIdentityAsync(
user, DefaultAuthenticationTypes.ApplicationCookie);
AuthenticationManager.SignIn(
new AuthenticationProperties() {
IsPersistent = isPersistent
}, identity);
}
Run Code Online (Sandbox Code Playgroud)