1

I've created an ASP.Net MVC4 web application and it includes the templated functionality that allows users to register with external providers such as Facebook and Twitter. This uses OAuth and SimpleMembership. I'm using Entity Framework code-first, which I'm new to, so I'm finding it difficult to do something really simple.

Once the user has registered with the external provider, a record is created in webpages_OAuthMembership with fields Provider, ProviderUserId and UserId. UserId maps to UserId in the UserProfile table. How do I read the ProviderUserId for the authenticated user? I need it to for use with the FB.api and for other things like retrieving the user photo using https://graph.facebook.com/[ProviderUserId]/picture?type=small.

I have tried this:

SimpleMembershipProvider provider = (SimpleMembershipProvider)Membership.Provider;
string providerUserId = provider.GetUser("[username]",  true).ProviderUserKey.ToString();

but ProviderUserKey just returns the UserId rather than ProviderUserId.

There must be a simple way to do this that I'm missing?

Liam Weston
  • 233
  • 1
  • 3
  • 11

1 Answers1

1

This answer assumes using Entity Framework 6 Code First approach (although all the code should work with earlier versions of EF).

I was having issues with accessing Provider and ProviderUserId too. I was attempting to delete a user accounts that authenticated with OAuth. I was able to delete the actual user account from the database with the code:

((SimpleMembershipProvider)Membership.Provider).DeleteAccount(selectedUser); // deletes record from webpages_Membership table
((SimpleMembershipProvider)Membership.Provider).DeleteUser(selectedUser, true); // deletes record from UserProfile table

However, this left the entry in the webpages_OAuthMembership table (which I wanted to delete!).

To solve this, I made a class with the same name as the db table:

public class webpages_OAuthMembership
{
    [Key, Column(Order=0)]
    public string Provider { get; set; }
    [Key, Column(Order = 1)]
    public string ProviderUserId { get; set; }
    public int UserId { get; set; }
}

The Key and Column data annotations let EF know that Provider and ProviderUserId form a composite key (I think SQL Server calls it a clustered PK). Anyways, in the DbContext class that initializes the database, I did a DbSet on that class:

public DbSet<webpages_OAuthMembership> webpages_OAuthMembership { get; set; }

This now allows you to access the OAuth table in the database in your code

var db = new DbContext();  db.webpages_OAuthMembership.ToList();
// this would give you all entries in the OAuth table

For your case, to get the ProviderUserId, you could use the following code

var OAuthAccount = db.webpages_OAuthMembership.Where(u => u.UserId == userIdOfUserYouAreSearchingFor).FirstOrDefault;
// ProviderUserId would be OAuthAccount.ProviderUserId

where 'userIdOfUserYouAreSearchingFor' is the UserId (from UserProfile table). Make sure to have the FirstOrDefault on the end. That way, if there is no entry in the database for that particular UserId, it will receive null and you can check again that.

Now in my case, I wanted to delete entries from said table. Just in case anyone else comes across this, I will include that info too. I used this code

var OAuthAcct = ((SimpleMembershipProvider)Membership.Provider).GetAccountsForUser(selectedUser).ToList();
var provider = OAuthAcct[0].Provider;
var providerUserId = OAuthAcct[0].ProviderUserId;
((SimpleMembershipProvider)Membership.Provider).DeleteOAuthAccount(provider, providerUserId);

to delete entries from the webpages_OAuthMembership table.

Hope that helps!

pmalbu
  • 935
  • 9
  • 13