ClientRoleProvider.IsUserInRole(String, String) メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
指定したユーザーが指定したロールに存在するかどうかを示す値を取得します。
public:
override bool IsUserInRole(System::String ^ username, System::String ^ roleName);
public override bool IsUserInRole (string username, string roleName);
override this.IsUserInRole : string * string -> bool
Public Overrides Function IsUserInRole (username As String, roleName As String) As Boolean
パラメーター
- username
- String
ユーザーの名前です。
- roleName
- String
ロールの名前。
戻り値
指定したユーザーが指定したロール内の場合は true
、指定したユーザーが指定したロール内でない場合または認証されない場合は false
。
例外
例
次のコード例は、このメソッドに直接アクセスして、ユーザーが特定のロールに含まれているかどうかを判断する方法を示しています。 このコードでは、まずユーザー ログインの有効期限が切れているかどうかをテストします。 メソッドを呼び出すには明示的 ClientRoleProvider な参照が GetRolesForUser 必要であるため、同じ参照を使用して メソッドを IsUserInRole 呼び出します。 ユーザーが "マネージャー" ロールの場合、このコードはメソッドを PerformManagerTask
呼び出しますが、これは指定されていません。
private void AttemptManagerTask()
{
System.Security.Principal.IIdentity identity =
System.Threading.Thread.CurrentPrincipal.Identity;
// Return if the authentication type is not "ClientForms".
// This indicates that the user is logged out.
if (!identity.AuthenticationType.Equals("ClientForms")) return;
try
{
ClientRoleProvider provider =
(ClientRoleProvider)System.Web.Security.Roles.Provider;
String userName = identity.Name;
// Determine whether the user login has expired by attempting
// to retrieve roles from the service. Call the ResetCache method
// to ensure that the roles are retrieved from the service. If no
// roles are returned, then the login has expired. This assumes
// that every valid user has been assigned to one or more roles.
provider.ResetCache();
String[] roles = provider.GetRolesForUser(userName);
if (roles.Length == 0)
{
MessageBox.Show(
"Your login has expired. Please log in again to access " +
"the roles service.", "Attempting to access user roles...");
// Call ValidateUser with empty strings in order to
// display the login dialog box configured as a
// credentials provider.
if (!System.Web.Security.Membership.ValidateUser(
String.Empty, String.Empty))
{
MessageBox.Show("Unable to authenticate. " +
"Cannot retrieve user roles.", "Not logged in",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
if (provider.IsUserInRole(userName, "manager"))
{
PerformManagerTask();
}
}
catch (System.Net.WebException)
{
MessageBox.Show(
"Unable to access the remote service. " +
"Cannot retrieve user roles.", "Warning",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
Private Sub AttemptManagerTask()
Dim identity As System.Security.Principal.IIdentity = _
System.Threading.Thread.CurrentPrincipal.Identity
' Return if the authentication type is not "ClientForms".
' This indicates that the user is logged out.
If Not identity.AuthenticationType.Equals("ClientForms") Then Return
Try
Dim provider As ClientRoleProvider = _
CType(System.Web.Security.Roles.Provider, ClientRoleProvider)
Dim userName As String = identity.Name
' Determine whether the user login has expired by attempting
' to retrieve roles from the service. Call the ResetCache method
' to ensure that the roles are retrieved from the service. If no
' roles are returned, then the login has expired. This assumes
' that every valid user has been assigned to one or more roles.
provider.ResetCache()
Dim roles As String() = provider.GetRolesForUser(userName)
If roles.Length = 0 Then
MessageBox.Show( _
"Your login has expired. Please log in again to access " & _
"the roles service.", "Attempting to access user roles...")
' Call ValidateUser with empty strings in order to
' display the login dialog box configured as a
' credentials provider.
If Not System.Web.Security.Membership.ValidateUser( _
String.Empty, String.Empty) Then
MessageBox.Show("Unable to authenticate. " & _
"Cannot retrieve user roles.", "Not logged in", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End If
End If
If provider.IsUserInRole(userName, "manager") Then
PerformManagerTask()
End If
Catch ex As System.Net.WebException
MessageBox.Show( _
"Unable to access the remote service. " & _
"Cannot retrieve user roles.", "Warning", _
MessageBoxButtons.OK, MessageBoxIcon.Warning)
End Try
End Sub
注釈
認証されたユーザーが特定のロールにあるかどうかを確認するには、 プロパティによって返される のメソッドをIPrincipalstatic
Thread.CurrentPrincipal呼び出IsInRoleします。 クライアント アプリケーション サービスを使用するように構成されたアプリケーションで、このプロパティは ClientRolePrincipal を返します。 このクラスは IPrincipal インターフェイスを実装しているため、明示的に参照する必要はありません。 メソッドは ClientRolePrincipal.IsInRole 内部的に メソッドを IsUserInRole 呼び出します。 メソッドは IsUserInRole 、 メソッドを GetRolesForUser 使用して、 によって username
示されるユーザーが によって roleName
示されるロールに含まれているかどうかを判断します。
サービス プロバイダーは、不要なサービス呼び出しを回避するために、ローカル ファイル システムに関するロール情報をキャッシュします。 詳細については、クラスの概要に関するページを ClientRoleProvider 参照してください。
適用対象
こちらもご覧ください
.NET
フィードバック
https://aka.ms/ContentUserFeedback」を参照してください。
以下は間もなく提供いたします。2024 年を通じて、コンテンツのフィードバック メカニズムとして GitHub の issue を段階的に廃止し、新しいフィードバック システムに置き換えます。 詳細については、「フィードバックの送信と表示