OAuthWebSecurity创建帐户 - 它是如何工作的?

时间:2013-01-28 07:15:24

标签: c# asp.net asp.net-mvc asp.net-mvc-4 dotnetopenauth

我很困惑并关注以下行,因为OAuthWebSecurity的API似乎有自己的身份验证存储。

        // If the current user is logged in add the new account
        OAuthWebSecurity.CreateOrUpdateAccount(result.Provider, result.ProviderUserId, User.Identity.Name);

如果我正确阅读上述内容,似乎表明API在本地保存了关系。

请告诉我情况并非如此,并解释它究竟是做什么的?我需要我的Web应用程序尽可能无状态,我不能让API存储像这样的本地值。

1 个答案:

答案 0 :(得分:5)

它使用作为ASP.NET MVC 4中的默认提供程序的SimpleMembershipProvider来创建或更新公共提供程序用户标识与本地用户之间的关联。基本上它会在webpages_OAuthMembership表中添加记录。

以下是来自WebSecurity.CreateOrUpdateOAuthAccount的相应代码:

public override void CreateOrUpdateOAuthAccount(string provider, string providerUserId, string userName)
{
    this.VerifyInitialized();
    if (userName.IsEmpty())
    {
        throw new MembershipCreateUserException(MembershipCreateStatus.ProviderError);
    }
    int userId = this.GetUserId(userName);
    if (userId == -1)
    {
        throw new MembershipCreateUserException(MembershipCreateStatus.InvalidUserName);
    }
    int userIdFromOAuth = this.GetUserIdFromOAuth(provider, providerUserId);
    using (IDatabase database = this.ConnectToDatabase())
    {
        if (userIdFromOAuth == -1)
        {
            if (database.Execute("INSERT INTO [" + OAuthMembershipTableName + "] (Provider, ProviderUserId, UserId) VALUES (@0, @1, @2)", new object[] { provider, providerUserId, userId }) != 1)
            {
                throw new MembershipCreateUserException(MembershipCreateStatus.ProviderError);
            }
        }
        else if (database.Execute("UPDATE [" + OAuthMembershipTableName + "] SET UserId = @2 WHERE UPPER(Provider)=@0 AND UPPER(ProviderUserId)=@1", new object[] { provider, providerUserId, userId }) != 1)
        {
            throw new MembershipCreateUserException(MembershipCreateStatus.ProviderError);
        }
    }
}