0

I have a VS 2013 project using Entity framework with webforms and MVC. I have been struggling to find VB.NET code that will retrieve extra data from the external provider when I use google or other external logins. There is plenty of comments in c# code for this, specifically article(s):-

How do i retrieve the email address when using google auth in mvc 5?

Getting the email from external providers Google and Facebook during account association step in a default MVC5 app

Well I have an answer and thought I'd share it. Placed in the login.aspx.vb file. Having authenticated the login you can use the following to get the extra data such as email

If User.Identity.IsAuthenticated Then

    Dim verifiedloginInfo = Context.GetOwinContext().Authentication.GetExternalLoginInfo(IdentityHelper.XsrfKey, User.Identity.GetUserId())

    Dim externalIdentity = Context.GetOwinContext().Authentication.GetExternalIdentityAsync(DefaultAuthenticationTypes.ExternalCookie)
    Dim emailClaim = externalIdentity.Result.Claims.FirstOrDefault(Function(c) c.Type = ClaimTypes.Email)
    Dim email = emailClaim.Value

    MsgBox(email)

end if

Apologies for breaking the forum etiquette but as a newbie I can’t comment on other posts.. what’s a boy to do. Special thanks to this utility http://codeconverter.sharpdevelop.net/SnippetConverter.aspx>

Community
  • 1
  • 1
ian
  • 1
  • 1
  • REVIEW: Removed the code from login.aspx.vb and inserted into RegisterExternalLogin.aspx.vb. This enabled me to allow athentication of the user by social media, validate the username, create a user. Hand add the claim value(s) for email to the new user profile. – ian Feb 14 '14 at 16:47
  • So behind the Login_click call I placed:- ` IdentityHelper.SignIn(manager, user, False) Dim externalIdentity = Context.GetOwinContext().Authentication.GetExternalIdentityAsync(DefaultAuthenticationTypes.ExternalCookie) Dim emailClaim = externalIdentity.Result.Claims.FirstOrDefault(Function(c) c.Type = ClaimTypes.Email) If manager.AddClaim(user.Id, emailClaim).Succeeded Then ‘ Add anything extra here if you wish else ‘ Add extra here if you wish End If IdentityHelper.RedirectToReturnUrl(Request.QueryString("ReturnUrl"), Response) ` Hope this helps others – ian Feb 14 '14 at 16:49

0 Answers0