ClientRoleProvider.GetRolesForUser(String) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得指定用戶所屬的角色名稱。
public:
override cli::array <System::String ^> ^ GetRolesForUser(System::String ^ username);
public override string[] GetRolesForUser (string username);
override this.GetRolesForUser : string -> string[]
Public Overrides Function GetRolesForUser (username As String) As String()
參數
- username
- String
要為其擷取角色的用戶名稱。
傳回
String[]
username
所屬的角色名稱,如果未驗證使用者,則為空數位列。
例外狀況
範例
下列範例程式代碼示範如何使用此方法來判斷使用者登入是否在測試角色成員資格之前已過期。 此程式代碼假設所有有效的使用者都與一或多個角色相關聯。 在此情況下,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
備註
GetRolesForUser 方法會擷取目前已驗證使用者的角色資訊,您必須在 username
參數中指定。 您可以透過 static
Thread.CurrentPrincipal 屬性取得使用者名稱,如下所示:Thread.CurrentPrincipal.Identity.Name
。
服務提供者會快取本機文件系統的角色資訊,以避免不必要的服務呼叫。 如需詳細資訊,請參閱 ClientRoleProvider 類別概觀。