ClientRoleProvider.IsUserInRole(String, String) Metod
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Hämtar ett värde som anger om den angivna användaren finns i den angivna rollen.
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
Parametrar
- username
- String
Namnet på användaren.
- roleName
- String
Namnet på rollen.
Returer
true om den angivna användaren är i den angivna rollen; false om den angivna användaren inte är autentiserad eller inte finns i den angivna rollen.
Undantag
username är Empty eller null.
-eller-
username är inte användarnamnet för den aktuella autentiserade användaren.
Exempel
Följande exempelkod visar hur du kommer åt den här metoden direkt för att avgöra om användaren har en viss roll. Den här koden testar först om användarinloggningen har upphört att gälla. En explicit ClientRoleProvider referens krävs för att anropa GetRolesForUser metoden, så samma referens används för att anropa IsUserInRole metoden. Om användaren har rollen "chef" anropar den här koden en PerformManagerTask metod som inte tillhandahålls.
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
Kommentarer
Du kan avgöra om en autentiserad IsInRole användare har en viss roll genom att anropa metoden för den IPrincipal som returneras av staticThread.CurrentPrincipal egenskapen. För program som har konfigurerats för att använda klientprogramtjänster returnerar den här egenskapen en ClientRolePrincipal. Eftersom den här klassen implementerar IPrincipal gränssnittet behöver du inte uttryckligen referera till det. Metoden ClientRolePrincipal.IsInRole anropar IsUserInRole metoden internt. Metoden IsUserInRole använder GetRolesForUser metoden för att avgöra om användaren som anges av username är i den roll som anges av roleName.
Tjänstleverantören cachelagrar rollinformation om det lokala filsystemet för att undvika onödiga tjänstanrop. Mer information finns i klassöversikten ClientRoleProvider .