通过OAuth登录用户后,我无法获得用户ID的MVC [英] MVC Simplemembership I cannot get the userID after logging a user in via OAuth
问题描述
所以我正在通过MVC 4 simplemembership oAuth提供程序实现Facebook登录.
So I am in the process of implementing a Facebook login via MVC 4 simplemembership oAuth provider.
注册和登录用户效果很好.但是,在使用oAth登录用户后,我无法获取经过身份验证的用户的UserId.我可以看到MemberId_oauth表中存在UserId,它与UserProfile表中的UserId相关.
Registering and signing in the user has worked well. However after signing in a user using oAth, I am unable to get the UserId of the authenticated user. I can see that the UserId exists in the membership_oauth table and it relates to the UserId in the UserProfile table.
通常,我会使用WebSecurity.CurrentUserId来获取它,但似乎没有使用OAuthWebSecurity时获取此方法的方法.当我尝试WebSecurity.CurrentUserId时,我得到该对象的实例尚未实例化",错误.
Normally I would fetch it using WebSecurity.CurrentUserId but there does not seem to be a method for getting this when using OAuthWebSecurity. When I try, WebSecurity.CurrentUserId I get "an instance of the object has not been instantiated", error.
有人知道如何获取此信息吗?
Does anybody know how to get this information?
非常感谢!
推荐答案
我假设您正在尝试在控制器中获取userID.您是否尝试过:
I assume you're trying to get the userID in in a controller. Have you tried:
int currentUserId = (int) Membership.GetUser().ProviderUserKey;
我无法告诉您这是否适用于Facebook,但适用于Google Auth.对我来说.
I can't tell you if this works with Facebook or not, but it works with Google Auth. for me.
如果您使用的不是AccountController,则还应该在控制器类的顶部添加 [InitializeSimpleMembership]
.
You should also add [InitializeSimpleMembership]
on top of controller class if you use another controller than AccountController.
检查此内容:在新的MVC4应用程序中获取UserId(int)
Check this method int GetUserIdFromOAuth(string provider, string providerUserId)
and try something like:
SimpleMembershipProvider provider = (SimpleMembershipProvider) Membership.Provider;
int id = provider.GetUserIdFromOAuth("you_know_the_provider", "you_can_get_the_provider_user_id")
另外,这个问题的人:
OAuth和SimpleMembership-如何从中检索ProviderUserIdwebpages_OAuthMembership表
抱怨与您的问题相反:他想要ProviderUserKey,但一直获取UserID.
is complaining about the opposite of your problem: he wants the ProviderUserKey but keeps getting the UserID instead.
这篇关于通过OAuth登录用户后,我无法获得用户ID的MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!