2

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!

Julian Dormon
  • 1,767
  • 4
  • 32
  • 58

1 Answers1

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.

Community
  • 1
  • 1
user1987392
  • 3,921
  • 4
  • 34
  • 59
  • Thanks. Membership.GetUser().ProviderUserKey; returns the username provided by facebook, I'm trying to get the UserId that links the membership_oauth table to the UserProfile table. It is an int. – Julian Dormon Nov 29 '13 at 14:03