How to get UserID in OWIN OAuth.NET

I am following this article to achieve OAuth authentication: http://www.asp.net/mvc/tutorials/mvc-5/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2-and -openid-sign-on

You can find the ExternalLoginCallback controller action. This activity runs the following method:

ExternalLoginInfo loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();

      

loginInfo has a login property. The Login property has properties LoginProvider and ProviderKey.

Does anyone know what is the ProviderKey property? Is it a unique UserID registered with the provider?

thank

+3


source to share


1 answer


try the User.Identity.GetUserId()

method.

if (loginInfo == null)
{
    return View("ExternalLoginFailure");
}

var userId = User.Identity.GetUserId();

      

for more information, see Get more information from social providers used in VS 2013 project templates .



Update: Another way to get the user:

var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
if (loginInfo == null)
{
    return RedirectToAction("Login");
}

var user = await UserManager.FindAsync(loginInfo.Login);
if (user != null)
{
    await SignInAsync(user, isPersistent: false);
    return RedirectToLocal(returnUrl);
}

      

+7


source







All Articles