2010-01-14 32 views
1

我正在用簡單的用戶身份驗證方案構建一個小型Web應用程序。我註冊的用戶在web.config中,像這樣的:使用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中配置我的角色?如果是的話,你碰巧知道任何可用的實現?

回答

3

我創建了一個使用web.config存儲的iRoleProvider的基本實現。檢查Codeplex,Web.Config Role Provider

+0

偉大的解決方案 - 感謝您的工具。正是我需要的。 – AhabLives

1

這似乎已經在前面闡述:Adding Role to User Created in 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對象來覆蓋角色。

相關問題