ClientRoleProvider.IsUserInRole(String, String) Metoda
Definicja
Ważne
Niektóre informacje odnoszą się do produktu w wersji wstępnej, który może zostać znacząco zmodyfikowany przed wydaniem. Firma Microsoft nie udziela żadnych gwarancji, jawnych lub domniemanych, w odniesieniu do informacji podanych w tym miejscu.
Pobiera wartość wskazującą, czy określony użytkownik znajduje się w określonej roli.
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
Parametry
- username
- String
Nazwa użytkownika.
- roleName
- String
Nazwa roli.
Zwraca
true
jeśli określony użytkownik znajduje się w określonej roli; false
jeśli określony użytkownik nie jest uwierzytelniony lub nie znajduje się w określonej roli.
Wyjątki
username
to Empty lub null
.
-lub-
username
nie jest nazwą użytkownika bieżącego, uwierzytelnionego użytkownika.
Przykłady
Poniższy przykładowy kod pokazuje, jak uzyskać bezpośredni dostęp do tej metody, aby określić, czy użytkownik jest w określonej roli. Ten kod najpierw sprawdza, czy logowanie użytkownika wygasło. Do wywołania GetRolesForUser metody jest wymagane jawne ClientRoleProvider odwołanie, więc to samo odwołanie jest używane do wywoływania IsUserInRole metody. Jeśli użytkownik znajduje się w roli "menedżer", ten kod wywołuje metodę PerformManagerTask
, która nie jest podana.
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
Uwagi
Możesz określić, czy uwierzytelniony użytkownik jest w określonej roli, wywołując IsInRole metodę IPrincipal zwracaną przez static
Thread.CurrentPrincipal właściwość . W przypadku aplikacji skonfigurowanych do korzystania z usług aplikacji klienckich ta właściwość zwraca ClientRolePrincipalwartość . Ponieważ ta klasa implementuje IPrincipal interfejs, nie trzeba odwoływać się do niego jawnie. Metoda ClientRolePrincipal.IsInRole wewnętrznie wywołuje metodę IsUserInRole . Metoda IsUserInRole używa GetRolesForUser metody , aby określić, czy użytkownik wskazany przez username
element znajduje się w roli wskazanej przez roleName
.
Dostawca usług buforuje informacje o roli dotyczące lokalnego systemu plików, aby uniknąć niepotrzebnych wywołań usługi. Aby uzyskać więcej informacji, zobacz ClientRoleProvider omówienie klasy.
Dotyczy
Zobacz też
Opinia
https://aka.ms/ContentUserFeedback.
Dostępne już wkrótce: W 2024 r. będziemy stopniowo wycofywać zgłoszenia z serwisu GitHub jako mechanizm przesyłania opinii na temat zawartości i zastępować go nowym systemem opinii. Aby uzyskać więcej informacji, sprawdź:Prześlij i wyświetl opinię dla