2

我正在尝试将 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;
    }
}

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

此登录结构主要来自启动 MVC 4 互联网应用程序时给出的默认会员代码。

4

1 回答 1

5

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

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

string username = result.UserName;
string name = result.ExtraData["name"];

您需要将身份提供者发回的数据存储在您的数据库中(可能是电话号码、姓名等),这也是您生成 UserID 的地方。可以在此处找到更多信息(以及入门设置指南):

于 2013-06-18T04:21:48.027 回答