WindowsTokenRoleProvider.IsUserInRole Yöntem
Tanım
Önemli
Bazı bilgiler ürünün ön sürümüyle ilgilidir ve sürüm öncesinde önemli değişiklikler yapılmış olabilir. Burada verilen bilgilerle ilgili olarak Microsoft açık veya zımni hiçbir garanti vermez.
Belirtilen kullanıcının belirtilen yerleşik Windows rolünde olup olmadığını belirten bir değer alır.
Aşırı Yüklemeler
IsUserInRole(String, WindowsBuiltInRole) |
Belirtilen kullanıcının belirtilen yerleşik Windows rolünde olup olmadığını belirten bir değer alır. |
IsUserInRole(String, String) |
Belirtilen kullanıcının belirtilen Windows grubunda olup olmadığını belirten bir değer alır. |
IsUserInRole(String, WindowsBuiltInRole)
Belirtilen kullanıcının belirtilen yerleşik Windows rolünde olup olmadığını belirten bir değer alır.
public:
bool IsUserInRole(System::String ^ username, System::Security::Principal::WindowsBuiltInRole role);
public bool IsUserInRole (string username, System.Security.Principal.WindowsBuiltInRole role);
override this.IsUserInRole : string * System.Security.Principal.WindowsBuiltInRole -> bool
Public Function IsUserInRole (username As String, role As WindowsBuiltInRole) As Boolean
Parametreler
- username
- String
ETKİALANI\kullanıcıadı biçiminde aranacak kullanıcı adı.
- role
- WindowsBuiltInRole
Aranacak Windows rolü.
Döndürülenler
true
belirtilen kullanıcı belirtilen Windows rolündeyse; aksi takdirde , false
.
Özel durumlar
username
, null
değeridir.
Şu anda yürütülen kullanıcının öğesine Usereklenmiş kimliği doğrulanmış WindowsIdentity bir kullanıcısı yok. HTTP olmayan senaryolarda, şu anda yürütülen kullanıcının öğesine CurrentPrincipaleklenmiş kimliği doğrulanmış WindowsIdentity bir kullanıcısı yoktur.
-veya-
username
geçerli WindowsIdentityile eşleşmiyorName.
Örnekler
Aşağıdaki kod örneği, kullanıcının uygulama için rol bilgilerini görüntülemesine izin vermeden önce program aracılığıyla oturum açmış olan kullanıcının Yöneticiler rolünde olup olmadığını denetler. Rol yönetimini etkinleştiren bir Web.config dosyası örneği için bkz WindowsTokenRoleProvider. .
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Security" %>
<%@ Import Namespace="System.Security.Principal" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
string[] rolesArray;
public void Page_Load()
{
Msg.Text = "";
WindowsPrincipal p = (WindowsPrincipal)System.Threading.Thread.CurrentPrincipal;
if (!p.IsInRole(WindowsBuiltInRole.Administrator))
{
Msg.Text = "You are not authorized to view user roles.";
return;
}
// Bind roles to GridView.
try
{
rolesArray = Roles.GetRolesForUser(User.Identity.Name);
}
catch (HttpException e)
{
Msg.Text = "There is no current logged on user. Role membership cannot be verified.";
return;
}
UserRolesGrid.DataSource = rolesArray;
UserRolesGrid.DataBind();
UserRolesGrid.Columns[0].HeaderText = "Roles for " + User.Identity.Name;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Sample: View User Roles</title>
</head>
<body>
<form runat="server" id="PageForm">
<h3>View User Roles</h3>
<asp:Label id="Msg" ForeColor="maroon" runat="server" /><br />
<table border="0" cellspacing="4">
<tr>
<td valign="top"><asp:GridView runat="server" CellPadding="4" id="UserRolesGrid"
AutoGenerateColumns="false" Gridlines="None"
CellSpacing="0" >
<HeaderStyle BackColor="navy" ForeColor="white" />
<Columns>
<asp:TemplateField HeaderText="Roles" >
<ItemTemplate>
<%# Container.DataItem.ToString() %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView></td>
</tr>
</table>
</form>
</body>
</html>
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Web.Security" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Dim rolesArray() As String
Public Sub Page_Load()
Msg.Text = ""
Dim provider As WindowsTokenRoleProvider = CType(Roles.Provider, WindowsTokenRoleProvider)
If Not provider.IsUserInRole(User.Identity.Name, _
System.Security.Principal.WindowsBuiltInRole.Administrator) Then
Msg.Text = "You are not authorized to view user roles."
Return
End If
' Bind roles to GridView.
Try
rolesArray = Roles.GetRolesForUser(User.Identity.Name)
Catch e As HttpException
Msg.Text = "There is no current logged on user. Role membership cannot be verified."
Return
End Try
UserRolesGrid.DataSource = rolesArray
UserRolesGrid.DataBind()
UserRolesGrid.Columns(0).HeaderText = "Roles for " & User.Identity.Name
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Sample: View User Roles</title>
</head>
<body>
<form runat="server" id="PageForm">
<h3>View User Roles</h3>
<asp:Label id="Msg" ForeColor="maroon" runat="server" /><br />
<table border="0" cellspacing="4">
<tr>
<td valign="top"><asp:GridView runat="server" CellPadding="4" id="UserRolesGrid"
AutoGenerateColumns="false" Gridlines="None"
CellSpacing="0" >
<HeaderStyle BackColor="navy" ForeColor="white" />
<Columns>
<asp:TemplateField HeaderText="Roles" >
<ItemTemplate>
<%# Container.DataItem.ToString() %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView></td>
</tr>
</table>
</form>
</body>
</html>
Açıklamalar
IsUserInRole yöntemi, kullanıcının numaralandırma tarafından WindowsBuiltInRole açıklanan ortak Windows rollerinden birinde olup olmadığını denetlemenize olanak tanır. Bu yöntem, birden çok dilde yerelleştirilmiş uygulamalar için yararlıdır. Yönteminin IsUserInRole bu aşırı yüklemesi temel sınıfın RoleProvider bir parçası değildir ve yalnızca sınıfın ProviderRoles özelliği tür olarak WindowsTokenRoleProvider dönüştürülerek erişilebilir.
yöntemini yalnızca LOGON_USER sunucu değişkeni tarafından tanımlanan oturum açmış olan kullanıcı için çağırabilirsiniz IsUserInRole . parametresinde username
sağlanan değer şu anda oturum açmış olan kullanıcının adı değilse, bir HttpException oluşturulur.
IsUserInRole yöntemi yalnızca LOGON_USER sunucu değişkeni tarafından tanımlanan oturum açmış durumdaki kullanıcı için çağrılabilir. Oturum açmış geçerli kullanıcının Windows kimliği doğrulanmış bir kullanıcı olması gerekir. ASP.NET ve Windows kimlik doğrulaması hakkında daha fazla bilgi için bkz. ASP.NET Kimlik Doğrulaması.
Ayrıca bkz.
Şunlara uygulanır
IsUserInRole(String, String)
Belirtilen kullanıcının belirtilen Windows grubunda olup olmadığını belirten bir değer alır.
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
Parametreler
- username
- String
ETKİALANI\kullanıcıadı biçiminde aranacak kullanıcı adı.
- roleName
- String
ETKİALANI\roladı biçiminde aranacak Windows grubu.
Döndürülenler
true
belirtilen kullanıcı adı belirtilen Windows grubundaysa; aksi takdirde , false
.
Özel durumlar
Şu anda yürütülen kullanıcının öğesine Usereklenmiş kimliği doğrulanmış WindowsIdentity bir kullanıcısı yok. HTTP olmayan senaryolarda, şu anda yürütülen kullanıcının öğesine CurrentPrincipaleklenmiş kimliği doğrulanmış WindowsIdentity bir kullanıcısı yoktur.
-veya-
username
geçerli WindowsIdentityile eşleşmiyorName.
-veya-
Kullanıcının Windows grup bilgileri alınırken bir hata oluştu.
Örnekler
Aşağıdaki kod örneği, kullanıcının uygulama için rol bilgilerini görüntülemesine izin vermeden önce program aracılığıyla oturum açmış olan kullanıcının Yöneticiler rolünde olup olmadığını denetler. Rol yönetimini etkinleştiren bir Web.config dosyası örneği için bkz WindowsTokenRoleProvider. .
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Security" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
string[] rolesArray;
public void Page_Load()
{
Msg.Text = "";
try
{
if (!Roles.IsUserInRole(User.Identity.Name, @"BUILTIN\Administrators"))
{
Msg.Text = "You are not authorized to view user roles.";
return;
}
}
catch (HttpException e)
{
Msg.Text = "There is no current logged on user. Role membership cannot be verified.";
return;
}
// Bind roles to GridView.
rolesArray = Roles.GetRolesForUser(User.Identity.Name);
UserRolesGrid.DataSource = rolesArray;
UserRolesGrid.DataBind();
UserRolesGrid.Columns[0].HeaderText = "Roles for " + User.Identity.Name;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Sample: View User Roles</title>
</head>
<body>
<form runat="server" id="PageForm">
<h3>View User Roles</h3>
<asp:Label id="Msg" ForeColor="maroon" runat="server" /><br />
<table border="0" cellspacing="4">
<tr>
<td valign="top"><asp:GridView runat="server" CellPadding="4" id="UserRolesGrid"
AutoGenerateColumns="false" Gridlines="None"
CellSpacing="0" >
<HeaderStyle BackColor="navy" ForeColor="white" />
<Columns>
<asp:TemplateField HeaderText="Roles" >
<ItemTemplate>
<%# Container.DataItem.ToString() %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView></td>
</tr>
</table>
</form>
</body>
</html>
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Web.Security" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Dim rolesArray() As String
Public Sub Page_Load()
Msg.Text = ""
Try
If Not Roles.IsUserInRole(User.Identity.Name, "BUILTIN\Administrators") Then
Msg.Text = "You are not authorized to view user roles."
Return
End If
Catch e As HttpException
Msg.Text = "There is no current logged on user. Role membership cannot be verified."
Return
End Try
' Bind roles to GridView.
rolesArray = Roles.GetRolesForUser(User.Identity.Name)
UserRolesGrid.DataSource = rolesArray
UserRolesGrid.DataBind()
UserRolesGrid.Columns(0).HeaderText = "Roles for " & User.Identity.Name
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Sample: View User Roles</title>
</head>
<body>
<form runat="server" id="PageForm">
<h3>View User Roles</h3>
<asp:Label id="Msg" ForeColor="maroon" runat="server" /><br />
<table border="0" cellspacing="4">
<tr>
<td valign="top"><asp:GridView runat="server" CellPadding="4" id="UserRolesGrid"
AutoGenerateColumns="false" Gridlines="None"
CellSpacing="0" >
<HeaderStyle BackColor="navy" ForeColor="white" />
<Columns>
<asp:TemplateField HeaderText="Roles" >
<ItemTemplate>
<%# Container.DataItem.ToString() %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView></td>
</tr>
</table>
</form>
</body>
</html>
Açıklamalar
yöntemiIsUserInRole, bir kullanıcının Bir Windows grubunda olup olmadığını belirlemek için sınıfı ve IsInRole özelliğinin User yöntemi tarafından Roles çağrılır. yöntemini yalnızca LOGON_USER sunucu değişkeni tarafından tanımlanan oturum açmış olan kullanıcı için çağırabilirsiniz IsUserInRole . Oturum açmış geçerli kullanıcının Windows kimliği doğrulanmış bir kullanıcı olması gerekir. ASP.NET ve Windows kimlik doğrulaması hakkında daha fazla bilgi için bkz. ASP.NET Kimlik Doğrulaması.