如何在OWIN OAuth .NET中获取UserID

kat*_*330 3 asp.net-mvc oauth

我正在关注本文以实现OAuth身份验证:http: //www.asp.net/mvc/tutorials/mvc-5/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2-和OpenID的登录

您可以找到ExternalLoginCallback控制器的操作.在该操作中执行以下方法:

ExternalLoginInfo loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
Run Code Online (Sandbox Code Playgroud)

loginInfo有Login属性.Login属性具有LoginProvider和ProviderKey属性.

有谁知道它是什么ProviderKey属性?是注册到提供商的唯一UserID吗?

谢谢

Moh*_*our 7

尝试User.Identity.GetUserId()方法.

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

var userId = User.Identity.GetUserId();
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅VS 2013项目模板帖子中使用的社交提供程序获取更多信息.

更新:获取用户的另一种方式是:

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);
}
Run Code Online (Sandbox Code Playgroud)