Get ExtraData from MVC5 framework OAuth/OWin identity provider with external auth provider

前端 未结 7 2021
旧巷少年郎
旧巷少年郎 2020-11-30 20:22

I\'m trying to use the new MVC5 framework in VS 2013 preview.

The membership authentication framework has been overhauled and replaced with OWin.

<
7条回答
  •  执念已碎
    2020-11-30 20:50

    The following works for me for facebook:

    StartupAuth.cs:

    var facebookAuthenticationOptions = new FacebookAuthenticationOptions()
    {
        AppId = "x",
        AppSecret = "y"
    };
    facebookAuthenticationOptions.Scope.Add("email");
    app.UseFacebookAuthentication(facebookAuthenticationOptions);
    

    ExternalLoginCallback method:

    var externalIdentity = HttpContext.GetOwinContext().Authentication.GetExternalIdentityAsync(DefaultAuthenticationTypes.ExternalCookie);
    var emailClaim = externalIdentity.Result.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Email);
    var email = emailClaim.Value;
    

    And for Google:

    StartupAuth.cs

    app.UseGoogleAuthentication();
    

    ExternalLoginCallback method (same as for facebook):

    var externalIdentity = HttpContext.GetOwinContext().Authentication.GetExternalIdentityAsync(DefaultAuthenticationTypes.ExternalCookie);
    var emailClaim = externalIdentity.Result.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Email);
    var email = emailClaim.Value;
    

    If I set a breakpoint here:

    var email = emailClaim.Value;
    

    I see the email address for both facebook and Google in the debugger.

    Update: See this post instead for proper and complete solution; Getting the email from external providers Google and Facebook during account association step in a default MVC5 app

提交回复
热议问题