OWIN OAuth .NET环境下,如何通过asp.net-mvc获取用户UserID?
- 内容介绍
- 文章标签
- 相关推荐
本文共计264个文字,预计阅读时间需要2分钟。
我在关注实现OAuth身份验证的具体步骤,可以通过以下链接找到相关教程:[创建一个使用Facebook和Google OAuth2及OpenID登录的ASP.NET MVC 5应用程序](http://www.asp.net/mvc/tutorials/mvc-5/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2-and-openid-sign-on)。其中提到了ExternalLoginCallback控制器的作用。
我正在关注本文以实现OAuth身份验证:www.asp.net/mvc/tutorials/mvc-5/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2-and-openid-sign-on
您可以找到ExternalLoginCallback控制器的操作.
在该操作中执行以下方法:
ExternalLoginInfo loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
loginInfo有Login属性. Login属性具有LoginProvider和ProviderKey属性.
有谁知道它是什么ProviderKey属性?是注册到提供商的唯一UserID吗?
谢谢
尝试User.Identity.GetUserId()方法.if (loginInfo == null) { return View("ExternalLoginFailure"); } var userId = User.Identity.GetUserId();
有关更多信息,请参阅Get more information from Social providers used in the VS 2013 project templates帖子.
更新:获取用户的另一种方式是:
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); }
本文共计264个文字,预计阅读时间需要2分钟。
我在关注实现OAuth身份验证的具体步骤,可以通过以下链接找到相关教程:[创建一个使用Facebook和Google OAuth2及OpenID登录的ASP.NET MVC 5应用程序](http://www.asp.net/mvc/tutorials/mvc-5/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2-and-openid-sign-on)。其中提到了ExternalLoginCallback控制器的作用。
我正在关注本文以实现OAuth身份验证:www.asp.net/mvc/tutorials/mvc-5/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2-and-openid-sign-on
您可以找到ExternalLoginCallback控制器的操作.
在该操作中执行以下方法:
ExternalLoginInfo loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
loginInfo有Login属性. Login属性具有LoginProvider和ProviderKey属性.
有谁知道它是什么ProviderKey属性?是注册到提供商的唯一UserID吗?
谢谢
尝试User.Identity.GetUserId()方法.if (loginInfo == null) { return View("ExternalLoginFailure"); } var userId = User.Identity.GetUserId();
有关更多信息,请参阅Get more information from Social providers used in the VS 2013 project templates帖子.
更新:获取用户的另一种方式是:
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); }

