如果您已經在您的站點中使用SQL Server連接了身份驗證和成員資格,那麼您將很容易。只要按照下列步驟操作:
<connectionStrings>
<add name="MyLocalSQLServer"
connectionString="Initial Catalog=aspnetdb;
data source=.\sqlexpress;Integrated Security=SSPI;" />
</connectionStrings>
添加角色經理:
<system.web>
<roleManager enabled="true" defaultProvider="MySqlRoleProvider" >
<providers>
<clear/>
<add name="MySqlRoleProvider" connectionStringName="MyLocalSQLServer"
applicationName="MyAppName"
type="System.Web.Security.SqlRoleProvider" />
</providers>
</roleManager>
</system.web>
現在你可以
SQL服務器
aspnet_regsql -S .\SQLExpress -E -A r
-S specifies the server, which is (.\SQLExpress) in this example.
-E specifies to use Windows authentication to connect to SQL Server.
-A r specifies to add only the role provider feature.
添加連接字符串爲角色提供上創建角色存儲在你的頁面中使用基於角色的東西(假設你已經啓用了認證[窗口或表單])
測試ASPX:
All Defined Roles: <asp:Lable id="lblAllRoles" runat="server" /><br/>
Your Roles: <asp:label id="lblMyRoles" runat="server" /><br/>
代碼隱藏:
protected void Page_Load(object sender, EventArgs e)
{
//here we are supposing that user is already authenticated
StringBuilder sb=new StringBuilder();
//gets roles for currently authenticated user
var roles = Roles.GetRolesForUser();
foreach (var v in roles)
sb.Append(", " + v);
lblMyRoles.Text = sb.ToString();
sb.Remove(0,sb.Length);
string [] allRoles = Roles.GetAllRoles();
foreach(var v in allRoles)
sb.Append(", " + v);
lblAllRoles.Text = sb.ToString();
}
Simillarly可以使用Role.IsUserInRole("ROLE_NAME")
檢查,如果用戶分配特定角色。
@JMSA點擊下一步;) – 2009-09-06 16:08:35
@Rex M,我已經創建了DB,編寫了Web.config文件,並且需要一些C#代碼來生成身份驗證票證。 – anonymous 2009-09-06 16:15:05
@JMSA的';)'通常意味着這是一個笑話 – 2009-09-06 16:19:22