你可以使用下面的代碼。
Add功能ApplicationSignInManager類Identity.Config.cs
public override Task SignInAsync(ApplicationUser user, bool isPersistent, bool rememberBrowser)
{
var claims = new List<Claim>()
{
new Claim(ClaimTypes.NameIdentifier, user.Id),
new Claim(ClaimTypes.Email, user.Email)
};
this.AuthenticationManager.User.AddIdentity(new ClaimsIdentity(claims));
return base.SignInAsync(user, isPersistent, rememberBrowser);
}
嘗試的部份代碼
private ClaimsPrincipal GetCurrentUser()
{
var context = HttpContext.GetOwinContext();
if (context == null)
return null;
if (context.Authentication == null || context.Authentication.User == null)
return null;
return context.Authentication.User;
}
private string GetUserId()
{
var user = GetCurrentUser();
if (user == null)
return null;
var claim = user.Claims.FirstOrDefault(o => o.Type == ClaimTypes.NameIdentifier);
if (claim == null)
return null;
return claim.Value;
}
private string GetUserEmail()
{
var user = GetCurrentUser();
if (user == null)
return null;
var claim = user.Claims.FirstOrDefault(o => o.Type == ClaimTypes.Email);
if (claim == null)
return null;
return claim.Value;
}
你必須創建自定義ExternalSignInAsync methiod將返回一個模型,用戶ID和成功的結果。檢查安裝包Microsoft.AspNet.Identity.Samples nuget包瞭解更多詳細信息。 – DSR
[使用MVC 5的身份,登錄後無法獲取用戶名]的可能重複(http://stackoverflow.com/questions/30429373/using-mvc-5s-identity-cant-get-user-name-權利後登錄) –