組み込みのRoleProviderが付加価値をもたらすいくつかの方法を次に示します。
1:LoginView
コントロールはロールを使用して、さまざまなコンテンツをさまざまなロールに表示できるようにします。これを行うためにRoleProviderにフックします。
LoginViewコントロールでロールを使用する例:
<asp:LoginView id="LoginView1" runat="server">
<RoleGroups>
<asp:RoleGroup Roles="author">
<ContentTemplate>
some content here based on if user is in 'author' role....
</ContentTemplate>
</asp:RoleGroup>
<asp:RoleGroup Roles="editor">
<ContentTemplate>
some content here based on if user is in 'editor' role....
</ContentTemplate>
</asp:RoleGroup>
</RoleGroups>
</asp:LoginView>
2:次のようなweb.config設定により、サーバー上の物理パス(サブフォルダーなど)へのアクセスを許可できます。
<configuration>
<location path="MemberPages">
<system.web>
<authorization>
<allow roles="members, administrators" />
<deny users="*" />
</authorization>
</system.web>
</location>
<!-- other configuration settings here -->
<configuration>
3:ユーザーの役割を簡単に検出し、次のようなコードでアクションを実行できます。
if (User.IsInRole("members"))
{
//do something
}
else
{
//do something else
}
リストはどんどん増えていきます。この議論は正直に何度も行われてきました。独自の役割システムを作成して車輪の再発明をしないでください。抽象ロールプロバイダーを実装するだけで、それで完了します。これは、 ASP.NETでの役割管理の背景に関する優れた記事です。
編集:MVCでRoleProviderがどのように役立つかを実際に知りたいことを明確にした後、探しているものは次のとおりです。
ASP.NET MVC-ロールプロバイダーの代替?