OAuth登录后获取用户ID

Jor*_*dan 2 c# asp.net oauth asp.net-mvc-4

我正在尝试将OAuth注册/登录集成到我的ASP.NET MVC 4站点中.我登录后无法获取用户的ID和/或用户名.登录时,我有:

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

虽然OAuthWebSecurity.Login成功(返回true),但username设置为空字符串,userID设置为-1(这意味着没有人登录).任何人都可以帮助解释登录函数返回true之间的这种差异,但这两种确定当前用户的方式另有说法吗?或者也许告诉我一种在OAuth登录后找到UserId(对应于UserProfile表)的方法?

此登录结构主要来自启动MVC 4 Internet应用程序时提供的默认成员身份代码.

Dan*_*ale 5

我相信这取决于您使用的身份识别提供商.可以像这样检索用户名属性和身份提供者发送的特定密钥:

AuthenticationResult result = OAuthWebSecurity.VerifyAuthentication(Url.Action("ExternalLoginCallback", new { ReturnUrl = returnUrl }));

string username = result.UserName;
string name = result.ExtraData["name"];
Run Code Online (Sandbox Code Playgroud)

您将需要存储身份提供程序在数据库中发回的数据(可能是电话号码,姓名等),这也是您生成UserID的位置.更多信息(以及开始设置指南)可在此处找到: