2

Environment: MVC project using the standard web template from VS2013

When a user is signed in via an external login, eg Google, is it possible to retrieve information about the external login, eg LoginProvider, DefaultUserName, etc, from the Controller's User object (System.Security.Claims.ClaimsPrincipal)?

If not this object, is there any other means to get them within a MVC controller?

Old Geezer
  • 14,854
  • 31
  • 111
  • 198
  • I don't think it works from the `user` object directly. There is a really good explanation of how to retreive information for 3rd party login in this installment of the MVC5 fundamentas tutorial by Scott Allen, http://pluralsight.com/training/Player?author=scott-allen&name=aspdotnet-mvc5-fundamentals-m3-identity&mode=live&clip=0&course=aspdotnet-mvc5-fundamentals – EluciusFTW Mar 05 '15 at 08:29

1 Answers1

2

In the AcountsController look for the action called ExternalLoginCallback. There is a line of code as such

var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();

The loginInfo instance only contains only the basic information that your application needs to authenticate you. But if you call

var detailedLoginInfo = AuthenticationManager.AuthenticateAsync(DefaultAuthenticationTypes.ExternalCookie);

this instance should contain all the information you asked for.

EluciusFTW
  • 2,565
  • 6
  • 42
  • 59
  • Thanks. I asked this question because GetExternalLoginInfo didn't work for me as explained here: http://stackoverflow.com/questions/28812347/authenticationmanager-getexternallogininfo-returns-null-except-in-externalloginc. The AuthenticateAsync method in the PluralSight video also does not work outside ExternalLoginCallback. – Old Geezer Mar 05 '15 at 14:42
  • But could you not call that method there, in the `ExternalLoginCallback`, and store the information for later use? – EluciusFTW Mar 05 '15 at 14:43
  • Yes, that's what I would have to do. I wanted to avoid it because it would mean exposing everything on the query string parameters as control passes from one RedirectToAction to another. – Old Geezer Mar 05 '15 at 14:46
  • You could also store it in a Db, or put it in a static class – EluciusFTW Mar 05 '15 at 14:50