1

我正在使用简单的用户认证方案构建一个小型 Web 应用程序。我在 web.config 中注册用户,如下所示:

<authentication mode="Forms">
  <forms loginUrl="~/login.aspx" defaultUrl="default.aspx" ...>
    <credentials passwordFormat="SHA1">
      <user name="UserA" password="B60D121B438A380C343D5EC3C2037564B82FFEF3"/>
      <user name="UserB" password="B60D121B438A380C343D5EC3C2037564B82FFEF3"/>
    </credentials>
  </forms>
</authentication>

它工作得很好,我喜欢在这个特定的应用程序中不必依赖数据库但是,我很惊讶地发现您显然无法在同一个庄园中配置 web.config 中的角色 - 或者我在这里遗漏了一些非常明显的东西?

我真的必须实现自定义角色管理提供程序才能在 web.config 中配置我的角色吗?如果是,您是否碰巧知道任何可用的实现?

4

2 回答 2

3

我创建了 iRoleProvider 的基本实现,它使用 web.config 进行存储。在 Codeplex、 Web.Config Role Provider上查看。

于 2010-02-18T02:02:46.803 回答
1

这似乎已在之前得到解决:向 Web.config 中创建的用户添加角色

但是,如果您打算仅在 web.config 中执行此操作,则您可以在 web.config 中创建一个用于您自己的角色设置的部分。

<configuration>
    <configSections>
        <section name="UserRoles" type="System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" restartOnExternalChanges="true" requirePermission="false"/>
    </configSections>

    <UserRoles>
        <add key="UserA" value="Group1,Group2,Group3" />
        <add key="UserB" value="Group1,Group3" />
    </UserRoles>
<configuration>

然后您可以使用 global.asax 使用 Application_AuthenticationRequest 方法在您的用户对象中配置角色。我从未尝试过,但我想如果您想在 web.config 的授权元素中使用这些角色,您需要使用自定义 Principal 对象来覆盖这些角色。

于 2010-01-14T16:01:25.417 回答