C# ASP.net MVC 4 用户和角色管理(“Membership.Provider”属性必须是“ExtendedMembershipProvider”的实例。)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15663973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 17:31:23  来源:igfitidea点击:

ASP.net MVC 4 Users and Roles administration ("Membership.Provider" property must be an instance of "ExtendedMembershipProvider".)

c#asp.netasp.net-mvcasp.net-mvc-4

提问by oskar132

I've tried for 2 days to get something working but so far it's been pointless. What I need is to manage users and their roles, nothing else.

我已经尝试了 2 天来让某些东西正常工作,但到目前为止它毫无意义。我需要的是管理用户和他们的角色,没有别的。

What I've tried is to understand how to do it in MVC but I can't seem to find a detailed tutorial on how to do it, either on the internet or in any MVC 4 book.

我所尝试的是了解如何在 MVC 中执行此操作,但我似乎无法在 Internet 或任何 MVC 4 书籍中找到有关如何执行此操作的详细教程。

I started by enabling the aspnet database with the aspnet_regsql utility, this create these tables:

我首先使用 aspnet_regsql 实用程序启用 aspnet 数据库,这将创建以下表:

Tables added by aspnet_regsql

aspnet_regsql 添加的表

That's all I found on the tutorial, it does not show how to interact with these tables (which I assume is with the default membership provider), so far I've found there are 3 main options for doing this:

这就是我在教程中找到的全部内容,它没有显示如何与这些表(我假设是与默认成员资格提供程序)进行交互,到目前为止,我发现有 3 个主要选项可以执行此操作:

  • Default membership provider (from whom I know nothing)
  • Universal Providers
  • SimpleMembership
  • 默认会员提供商(我对此一无所知)
  • 通用供应商
  • 简单会员

I found this tutorial:

我找到了这个教程:

http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-7

http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-7

Here they ask to create a new MVC 4 application project with internet application template and copy all the files relating the account control.

在这里,他们要求使用 Internet 应用程序模板创建一个新的 MVC 4 应用程序项目,并复制与帐户控制相关的所有文件。

I did that for my project but as soon as I try to log in I get :

我为我的项目这样做了,但是一旦我尝试登录,我就会得到:

To call this method, the "Membership.Provider" property must be an instance of "ExtendedMembershipProvider".

要调用此方法,“Membership.Provider”属性必须是“ExtendedMembershipProvider”的实例。

which according to this forum: http://social.msdn.microsoft.com/Forums/en-US/windowsazuredevelopment/thread/d352bb1b-577c-42b7-8872-5ed59cd65f32/

根据这个论坛:http: //social.msdn.microsoft.com/Forums/en-US/windowsazuredevelopment/thread/d352bb1b-577c-42b7-8872-5ed59cd65f32/

is because of how I defined the profile, membership and roleManager providers on my web.config file which is defined as follows:

是因为我如何在 web.config 文件中定义配置文件、成员资格和 roleManager 提供程序,其定义如下:

<profile>
  <providers>
    <clear />
    <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="DatabaseConnection" applicationName="/" />
  </providers>
</profile>
<membership>
  <providers>
    <clear />
    <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="DatabaseConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
  </providers>
</membership>
<roleManager enabled="true">
  <providers>
    <clear />
    <add connectionStringName="DatabaseConnection" applicationName="/" name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" />
    <add applicationName="/" name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" />
  </providers>
</roleManager>

All I need is to be able to add new users, assign roles to them and show different content according to the role accessing the view.

我所需要的只是能够添加新用户,为他们分配角色并根据访问视图的角色显示不同的内容。

Question #1What is the membership provider being used when I create an MVC 4 application with an internet application template?

问题 #1当我使用 Internet 应用程序模板创建 MVC 4 应用程序时使用的会员提供程序是什么?

Question #2

问题2

What am I doing wrong with the web.config file? How can I define the providers correctly?

我对 web.config 文件做错了什么?如何正确定义提供者?

Question #3

问题#3

Which option suits me better? If you know a nice tutorial for it could you please link me to it?

哪个选项更适合我?如果你知道一个很好的教程,你可以把我链接到它吗?

Any help will be really appreciated! thanks in advance!

任何帮助将不胜感激!提前致谢!

采纳答案by Win

Membership provider you are using is dated. New Universal Membership Provideruses new table names without prefix aspnet_.

您使用的会员提供商已过时。新的Universal Membership Provider使用没有前缀的新表名aspnet_

1) Please take a look at Scott Hanselman's link.

1) 请看一下 Scott Hanselman 的链接。

2) You do not need profileif you do not use it. roleManagerconfiguration is not correct.

2)profile如果你不使用它,你就不需要。roleManager配置不正确。

FYI: Please define machineKeyexplicitly in web.config; it will save you a lot of headache in the future.

仅供参考:请machineKey在 web.config 中明确定义;它会在未来为您省去很多麻烦。

P.S. I agreed with you. I just finished reading Pro ASP.NET MVC 4 by Adam Freeman; he did not mention about Membership Provider. ASP.NET MVC 4 and the Web API by Jamie Kurtzuses legacy Membership Provider if you want to read.

PS 我同意你的看法。我刚读完Pro ASP.NET MVC 4 by Adam Freeman;他没有提到会员提供者。ASP.NET MVC 4 and the Web API by Jamie Kurtz如果您想阅读,请使用旧会员提供程序。

回答by Aaron Hoffman

Question #1 What is the membership provider being used when I create an MVC 4 application with an internet application template?

问题 #1 当我使用 Internet 应用程序模板创建 MVC 4 应用程序时使用的会员提供程序是什么?

Answer: SimpleMembershipProvider

答案:SimpleMembershipProvider

Question #2 What am I doing wrong with the web.config file? how can I define the providers correctly?

问题 #2 我在 web.config 文件中做错了什么?如何正确定义提供者?

Answer: For SimpleMembershipProvider, modifications to the web.config file are not necessary. See here for detailed configuration/setup information and tutorial: http://aaron-hoffman.blogspot.com/2013/02/aspnet-mvc-4-membership-users-passwords.html

答:对于 SimpleMembershipProvider,不需要修改 web.config 文件。有关详细配置/设置信息和教程,请参见此处:http: //aaron-hoffman.blogspot.com/2013/02/aspnet-mvc-4-membership-users-passwords.html

回答by Dmitry

Membership and security was extremely overcomplicated in ASP.Net and is still overcomplicated in ASP.NET MVC. But fortunately it's possible to not use built-in membership at all. I've created simple solutione and used in a lot of projects and now added it to github see http://github.com/TesserisPro/ASP.NET-SImple-Security-ProviderYou can easy customize it.

成员资格和安全性在 ASP.Net 中极其复杂,在 ASP.NET MVC 中仍然过于复杂。但幸运的是,完全可以不使用内置成员资格。我创建了简单的解决方案并在很多项目中使用,现在将其添加到 github 参见http://github.com/TesserisPro/ASP.NET-SImple-Security-Provider您可以轻松自定义它。