ASP.NET MVC 4如何使用自定义membershipprovider和roleprovider实现Oauth

时间:2022-09-02 13:29:26

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

经过大量的搜索和大量的尝试后,我终于想出了如何让人们在我的网站上注册和登录(给他们角色等并做授权)。我从类ExtendedMembershipProvider和RoleProvider继承,为他们创建了一个自定义类,但现在我仍然遇到让Oauth工作的问题(主要是OpenID)。我在AuthConfig中注册了GoogleClient,但是一旦我尝试登录并进入该行(在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)!

然后它说尚未实现,现在我检查了扩展成员资格,我不能覆盖登录(因为它不在那里,但它在哪里)。我也搜索谷歌很多,但没有运气,任何获得oauth工作的说明都会出汗(我想我需要制作第3个CustomOath提供者,但我无法找到继承者的内容)!

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

PS:我制作了一个custommembershiprpovder和customroleprovider,因为我想使用不同的数据库方案。

Maxim

格言

2 个解决方案

#1


11  

I had to override 3 more methods in my custom memberbershipprovider

我必须在我的自定义memberbershipprovider中覆盖另外3个方法

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.

现在它正在运作。 (我通过覆盖成员资格提供程序中的所有方法来解决这个问题,然后在每个方法上设置一个断点,并且在任何地方我根据自定义数据库填写方法。

#2


3  

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.

这不是如何使OAuth在您的方案中工作,但如果有帮助,OAuthWebSecurity.Login的源代码位于http://aspnetwebstack.codeplex.com/SourceControl/changeset/view/5cb74eb3b2f3#src/Microsoft.Web。 WebPages.OAuth / OAuthWebSecurity.cs以及如何使用SimpleMembership实现OAuth,您可以查看http://aspnetwebstack.codeplex.com/SourceControl/changeset/view/5cb74eb3b2f3#src/WebMatrix.WebData/SimpleMembershipProvider.cs。

Your project sounds cool, good luck.

你的项目听起来很酷,祝你好运。

#1


11  

I had to override 3 more methods in my custom memberbershipprovider

我必须在我的自定义memberbershipprovider中覆盖另外3个方法

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.

现在它正在运作。 (我通过覆盖成员资格提供程序中的所有方法来解决这个问题,然后在每个方法上设置一个断点,并且在任何地方我根据自定义数据库填写方法。

#2


3  

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.

这不是如何使OAuth在您的方案中工作,但如果有帮助,OAuthWebSecurity.Login的源代码位于http://aspnetwebstack.codeplex.com/SourceControl/changeset/view/5cb74eb3b2f3#src/Microsoft.Web。 WebPages.OAuth / OAuthWebSecurity.cs以及如何使用SimpleMembership实现OAuth,您可以查看http://aspnetwebstack.codeplex.com/SourceControl/changeset/view/5cb74eb3b2f3#src/WebMatrix.WebData/SimpleMembershipProvider.cs。

Your project sounds cool, good luck.

你的项目听起来很酷,祝你好运。