[英]User.Identity.IsAuthenticated always returns false
我正在使用ASP.NET WEB API 2實現REST API。我具有默認的AccountController實現,其中包含// GET api / Account / ExternalLogin的方法。
[OverrideAuthentication]
[HostAuthentication(DefaultAuthenticationTypes.ExternalCookie)]
[AllowAnonymous]
[Route("ExternalLogin", Name = "ExternalLogin")]
public async Task<IHttpActionResult> GetExternalLogin(string provider, string error = null)
{
if (error != null)
{
return Redirect(Url.Content("~/") + "#error=" + Uri.EscapeDataString(error));
}
if (!User.Identity.IsAuthenticated)
{
return new ChallengeResult(provider, this);
}
ExternalLoginData externalLogin = ExternalLoginData.FromIdentity(User.Identity as ClaimsIdentity);
if (externalLogin == null)
{
return InternalServerError();
}
if (externalLogin.LoginProvider != provider)
{
Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie);
return new ChallengeResult(provider, this);
}
ApplicationUser user = await UserManager.FindAsync(new UserLoginInfo(externalLogin.LoginProvider,
externalLogin.ProviderKey));
bool hasRegistered = user != null;
if (hasRegistered)
{
Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie);
ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(UserManager,
OAuthDefaults.AuthenticationType);
ClaimsIdentity cookieIdentity = await user.GenerateUserIdentityAsync(UserManager,
CookieAuthenticationDefaults.AuthenticationType);
AuthenticationProperties properties = ApplicationOAuthProvider.CreateProperties(user.UserName);
Authentication.SignIn(properties, oAuthIdentity, cookieIdentity);
}
else
{
IEnumerable<Claim> claims = externalLogin.GetClaims();
ClaimsIdentity identity = new ClaimsIdentity(claims, OAuthDefaults.AuthenticationType);
Authentication.SignIn(identity);
}
return Ok();
}
我瀏覽了Internet,沒有找到任何適用於這種情況的信息。
我使用的網址
https_://_www.dummydomain.com:43363 / API /帳號/ ExternalLogin提供商=谷歌&RESPONSE_TYPE =令牌的client_id =自&REDIRECT_URI = HTTPS%3A%2F%2Fwww.dummydomain.com%3A43363%2F&狀態= jI4zGXuaVvHI8qf9E0Nww3qBwke0YsYwD9AORwKBj3o1
每個外部服務(Google / FB)都可以正常運行。 我看到設置了AspNet.ExternalCookie,但重定向回我無權獲得
{
email:null,
hasRegistred: true,
loginProvaider: null
}
更新1
AppController
上的Request
屬性的Properties
字典不包含MS_UserPrincipal
。
請參閱所附的屏幕截圖。 屬性鍵
Request.Properties["MS_HttpContext"]
返回:(請參見屏幕快照) MS_HttpContextobject
它無法直接在APIController中使用HttpContext屬性。 為此,您必須使用System.Net.Http.HttpRequestMessage類型的Request屬性。 HttpRequestMessage具有屬性字典; 您會發現保存您的IPrincipal對象的鍵MS_UserPrincipal的值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.