C# 在OAuth登录后获取用户ID
我正在尝试将OAuth注册/登录集成到我的ASP.NETMVC4站点中。但我在登录后获取用户ID和/或用户名时遇到问题。登录时,我有:C# 在OAuth登录后获取用户ID,c#,asp.net,asp.net-mvc-4,oauth,C#,Asp.net,Asp.net Mvc 4,Oauth,我正在尝试将OAuth注册/登录集成到我的ASP.NETMVC4站点中。但我在登录后获取用户ID和/或用户名时遇到问题。登录时,我有: AuthenticationResult result = OAuthWebSecurity.VerifyAuthentication(Url.Action("ExternalLoginCallback", new { ReturnUrl = returnUrl })); if(result.IsSuccessful) { if(OAuthWebSec
AuthenticationResult result = OAuthWebSecurity.VerifyAuthentication(Url.Action("ExternalLoginCallback", new { ReturnUrl = returnUrl }));
if(result.IsSuccessful)
{
if(OAuthWebSecurity.Login(result.Provider, result.ProviderUserId, createPersistentCookie: false))
{
string username = User.Identity.Name;
int userID = WebSecurity.CurrentUserId;
}
}
虽然OAuthWebSecurity.Login成功(返回true),但username设置为空字符串,userID设置为-1(这意味着没有人登录)。有人能解释一下登录函数返回true与确定当前用户的这两种方法之间的差异吗?或者在OAuth登录后,给我展示一种查找用户ID(对应于UserProfile表)的方法
此登录结构主要来自启动MVC 4 internet应用程序时提供的默认成员代码。我相信这取决于您使用的身份提供程序。可以按如下方式检索标识提供程序发送的用户名属性和特定密钥:
AuthenticationResult result = OAuthWebSecurity.VerifyAuthentication(Url.Action("ExternalLoginCallback", new { ReturnUrl = returnUrl }));
string username = result.UserName;
string name = result.ExtraData["name"];
您需要将身份提供者发送回您的数据库的数据(可能是电话号码、姓名等)存储在数据库中,这也是生成您的用户ID的地方。更多信息(以及开始设置指南)可在此处找到: