Memberフォルダ配下のページはログインしていないユーザのアクセスを拒否するよう設定しました。
今回は新たにAdminフォルダを作成し、Adminフォルダ配下のページはAdminロールを持っているユーザのみアクセスを許可するように設定します。
前回のコードに追加していきます。
カスタム ロール プロバイダーの作成
まずカスタムロールプロバイダを作成します。App_Codeに「CustomRoleProvider.vb」クラスを追加します。
CustomRoleProviderクラスはRoleProviderクラスを継承します。
抽象メソッドをオーバーラドしたメソッドが追加されますのでGetRolesForUserメソッドにロールを取得するコードを書きます。
その他のメソッドはNotSupportedExceptionをスローするようにしておきます。
Public Class CustomRoleProvider
Inherits RoleProvider
Public Overrides Property ApplicationName() As String
Get
Throw New NotSupportedException
End Get
Set(ByVal value As String)
Throw New NotSupportedException
End Set
End Property
Public Overrides Function GetRolesForUser(ByVal username As String) As String()
'入力されたusernameを元に、ユーザのロールを取得します。
return new String(){"Admin"}
End Function
Public Overrides Sub AddUsersToRoles(ByVal usernames() As String, ByVal roleNames() As String)
Throw New NotSupportedException
End Sub
Public Overrides Sub CreateRole(ByVal roleName As String)
Throw New NotSupportedException
End Sub
Public Overrides Function DeleteRole(ByVal roleName As String, ByVal throwOnPopulatedRole As Boolean) As Boolean
Throw New NotSupportedException
End Function
Public Overrides Function FindUsersInRole(ByVal roleName As String, ByVal usernameToMatch As String) As String()
Throw New NotSupportedException
End Function
Public Overrides Function GetAllRoles() As String()
Throw New NotSupportedException
End Function
Public Overrides Function GetUsersInRole(ByVal roleName As String) As String()
Throw New NotSupportedException
End Function
Public Overrides Function IsUserInRole(ByVal username As String, ByVal roleName As String) As Boolean
Throw New NotSupportedException
End Function
Public Overrides Sub RemoveUsersFromRoles(ByVal usernames() As String, ByVal roleNames() As String)
Throw New NotSupportedException
End Sub
Public Overrides Function RoleExists(ByVal roleName As String) As Boolean
Throw New NotSupportedException
End Function
End Class
web.configを編集
作成したカスタムロールプロバイダを登録します。<!--ロールプロバイダ--> <roleManager enabled="true" defaultProvider="CustomRoleProvider"> <providers> <add name="CustomRoleProvider" type="CustomRoleProvider"/> </providers> </roleManager>
Adminフォルダ配下のページは非認証ユーザを拒否し、Adminロールのみ許可を与えます。
<!--adminフォルダ--> <location path="admin"> <system.web> <authorization> <deny users="?" /> <allow roles="Admin" /> </authorization> </system.web> </location>
<?xml version="1.0"?>
・・・省略
<system.web>
・・・省略
<!--認証方法 Form認証-->
<!--<authentication mode="Windows"/>-->
<authentication mode="Forms">
<forms loginUrl="Default.aspx"/>
</authentication>
<!--メンバシッププロバイダ-->
<membership defaultProvider="CustomMembershipProvider">
<providers>
<add name="CustomMembershipProvider" type="CustomMembershipProvider"/>
</providers>
</membership>
<!--ロールプロバイダ-->
<roleManager enabled="true" defaultProvider="CustomRoleProvider">
<providers>
<add name="CustomRoleProvider" type="CustomRoleProvider"/>
</providers>
</roleManager>
<!--すべてのユーザーにアクセス許可を与える-->
<authorization>
<allow users="*"/>
</authorization>
</system.web>
<!--**個別フォルダ・ページのセキュリティ**-->
<!--Memberフォルダ-->
<location path="Member">
<system.web>
<authorization>
<deny users="?" />
</authorization>
</system.web>
</location>
<!--adminフォルダ-->
<location path="Admin">
<system.web>
<authorization>
<deny users="?" />
<allow roles="Admin" />
</authorization>
</system.web>
</location>
</configuration>
1 件のコメント:
お世話になっております。
GetRolesForUserはどこから呼ばれるのでしょうか?
もしくは、戻り値を何にセットするとconfigの設定が有効になるのでしょうか?
よろしくお願いします。
コメントを投稿