MVC Simplemembership I cannot get the userID after logging a user in via OAuth

此生再无相见时 提交于 2019-12-11 01:03:34

问题


So I am in the process of implementing a Facebook login via MVC 4 simplemembership oAuth provider.

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.

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?

Much appreciated!


回答1:


I assume you're trying to get the userID in in a controller. Have you tried:

int currentUserId = (int) Membership.GetUser().ProviderUserKey;

I can't tell you if this works with Facebook or not, but it works with Google Auth. for me.

You should also add [InitializeSimpleMembership] on top of controller class if you use another controller than AccountController.

Check this: Get UserId (int) in new MVC4 application

EDIT:

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")

Also, the guy in this question:

OAuth and SimpleMembership - how to retrieve ProviderUserId from webpages_OAuthMembership table

is complaining about the opposite of your problem: he wants the ProviderUserKey but keeps getting the UserID instead.



来源:https://stackoverflow.com/questions/20276465/mvc-simplemembership-i-cannot-get-the-userid-after-logging-a-user-in-via-oauth

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!