【问题标题】:Membership Role Provider doesn't work with custom connection string?成员角色提供程序不适用于自定义连接字符串?
【发布时间】:2011-04-27 04:55:09
【问题描述】:

请帮助解决一个问题。 我使用 IIS7 配置了 Membership,它的表位于我自己的数据库中,使用 aspnet_regsql 实用程序创建,并且我正在使用自定义连接字符串来访问它。

这是与会员资格相关的 web.config 的一部分:

<connectionStrings>
    <add connectionString="Server=CORESERVER\SQLExpress;Database=Shop;User ID=Tema;Password=Matrix" name="CustomSqlConnection" />
</connectionStrings>
<profile enabled="true">
      <providers>
        <add name="CustomSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="CustomSqlConnection" />
      </providers>
</profile>
<roleManager defaultProvider="AspNetSqlRoleProvider" enabled="true">
      <providers>
        <add name="CustomSqlRoleProvider" type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="CustomSqlConnection" />
      </providers>
</roleManager>
<membership defaultProvider="CustomSqlMemberProvider">
      <providers>
        <add name="CustomSqlMemberProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="CustomSqlConnection" enablePasswordReset="true" enablePasswordRetrieval="false" passwordFormat="Hashed" requiresQuestionAndAnswer="true" requiresUniqueEmail="true" applicationName="/" maxInvalidPasswordAttempts="10" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" passwordStrengthRegularExpression="" />
      </providers>
</membership>
<authentication mode="Forms">
      <forms cookieless="UseCookies" loginUrl="login.aspx" name="WebShopAuthentication" protection="All" timeout="30" path="/" requireSSL="false" defaultUrl="~/admin/default.aspx" />
</authentication>
<authorization>
      <allow users="*" />
</authorization>

并且... 表单授权,获取用户和他的会员信息就可以了。 但是……获得角色总是 FALSE。

MembershipUser userData = Membership.GetUser(HttpContext.Current.User.Identity.Name);    // OK !!! IT IS GREAT :)
var a = new RolePrincipal(HttpContext.Current.User.Identity);
var aa = a.getRoles();  // {string[0]} - EMPTY!!!
var b = Roles.IsUserInRole("Administrator", "Administrator");  // FALSE!!!
var c = Roles.Providers["CustomSqlRoleProvider"].GetAllRoles();  // {string[0]} - EMPTY!!!
var d = Roles.IsUserInRole(HttpContext.Current.User.Identity.Name, "Administrator"); // FALSE!!!
var e = HttpContext.Current.User.IsInRole("Administrator"); // FALSE !!!

为什么????

我做错了什么???

【问题讨论】:

  • 抱歉问了一个愚蠢的问题,但您是否真的创建了任何角色并将它们分配给当前用户?
  • 其实是的。有:1 个提供者,其覆盖的连接字符串指向我的数据库,4 个角色 - 管理员、客户、经理、用户,以及 3 个名为管理员、经理、客户的用户。

标签: asp.net sql membership provider


【解决方案1】:

只是为了完善......授权工作正常并正确使用角色。我的 web.config 的另一部分:

<location path="Admin">
    <system.web>
      <pages styleSheetTheme="Admin" theme="Admin">
      </pages>
      <authorization>
        <deny users="?" />
        <allow roles="Administrator" />
      </authorization>
    </system.web>
    <appSettings>
      <add key="ThemeName" value="Admin" />
    </appSettings>
</location>

然后在代码中使用:

Membership.ValidateUser(userName.Text, userPassword.Text) // AND IT WORKS - USER IS LOGGED IN

【讨论】:

    【解决方案2】:

    答案是我没有正确地将 applicationName 参数添加到 web.config - 添加后我应该重新启动 IIS 并在需要时重新创建角色。 这是 web.config 的最终版本:

    <roleManager defaultProvider="CustomSqlRoleProvider" enabled="true">
          <providers>
            <add name="CustomSqlRoleProvider" type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="CustomSqlConnection" applicationName="/" />
          </providers>
        </roleManager>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多