Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC 4 how to implement Oauth with custom membershipprovider and roleprovider

After alot of searching and alot of trying I finnaly figuered out how to let people register and login on my site (and give them role etc and do authorization). I Inherited from the class ExtendedMembershipProvider and RoleProvider, made both for them a customclass, but now I am still having issues getting Oauth to work (mainly OpenID). I registered GoogleClient in the AuthConfig, but once I try to login and it comes to the line (In AccountController.cs):

    if (OAuthWebSecurity.Login(result.Provider, result.ProviderUserId, createPersistentCookie: false))

Then it says not yet implemented, now I checked extended membership and I can t override Login (since it isn t there, but where is it). I also searched google alot but no luck, any instructions for getting oauth to work with would be sweat(I think I need to make a 3th CustomOathprovider, but I am not able to find what to inherith from)!

PS: I made a custommembershiprpovder and customroleprovider because I want to use a diffrent Database scheme.

Maxim

like image 410
Maximc Avatar asked Jan 31 '13 18:01

Maximc


2 Answers

I had to override 3 more methods in my custom memberbershipprovider

public override void CreateOrUpdateOAuthAccount(string provider, string providerUserId, string userName)

public override int GetUserIdFromOAuth(string provider, string providerUserId)
//return -1 if User got no OauthAccount

public override string GetUserNameFromId(int userId)

now it is working. (I figuered this out by overriding all methods in the membership provider, and then setting a breakpoint on each, and everywhere it went I filled in the method based on my custom Database.

like image 86
Maximc Avatar answered Oct 17 '22 05:10

Maximc


This would be a comment if I had enough reputation to comment.

This is not how to make OAuth work in your scenario, but in case it helps, the source for OAuthWebSecurity.Login is at http://aspnetwebstack.codeplex.com/SourceControl/changeset/view/5cb74eb3b2f3#src/Microsoft.Web.WebPages.OAuth/OAuthWebSecurity.cs and to see how SimpleMembership implemented OAuth you can look at http://aspnetwebstack.codeplex.com/SourceControl/changeset/view/5cb74eb3b2f3#src/WebMatrix.WebData/SimpleMembershipProvider.cs.

Your project sounds cool, good luck.

like image 36
Paul Rasmussen Avatar answered Oct 17 '22 04:10

Paul Rasmussen