在完成所有登录过程后如何获得ExternalIdentity?

Bru*_*oLM 4 c# asp.net-mvc

我正在使用MVC 5,我可以使用Google成功登录.

我希望在登录过程后能够访问用户外部身份声明.我想在视图中访问来自用户的声明"图片".但是,如果我尝试运行此代码,它始终返回null.(登录过程除外 - 为mvc模板自动生成的代码)

外部信息为空

我有办法访问外部身份声明吗?(登录后)

Bru*_*oLM 7

我发现了如何创建身份.基本上ExternalSignInAsync是内部呼叫进行SignInAsync呼叫CreateUserIdentityAsync.

ApplicationSignInManagerIdentityConfig文件中找到了一个类,然后我将CreateUserIdentityAsync方法更改为:

public override async Task<ClaimsIdentity> CreateUserIdentityAsync(ApplicationUser user)
{
    var externalIdentity = await AuthenticationManager.GetExternalIdentityAsync(DefaultAuthenticationTypes.ExternalCookie);

    var localIdentity = await user.GenerateUserIdentityAsync((ApplicationUserManager)UserManager);

    foreach (var item in externalIdentity.Claims)
    {
        if (!localIdentity.HasClaim(o => o.Type == item.Type))
            localIdentity.AddClaim(item);
    }

    return localIdentity;
}
Run Code Online (Sandbox Code Playgroud)

所以每次我登录时,我都会在登录用户中声明我的声明+外部声明.从我可以打电话来看:

@HttpContext.Current.GetOwinContext()
    .Authentication.User.FindFirst("urn:google:picture").Value
Run Code Online (Sandbox Code Playgroud)