【参加讨论】当使用 ASP.NET Windows 身份验证时,ASP.NET 将 WindowsPrincipal 对象附加到当前请求。该对象由 URL 身份验证使用。应用程序也可以以编程方式用它来确定请求标识是否在给定角色中。
if(User.IsInRole("Administrators")) {
DisplayPrivilegedContent();
}
If User.IsInRole("Administrators") Then
DisplayPrivilegedContent()
End If
if(User.IsInRole("Administrators")) {
DisplayPrivilegedContent();
}
|
| C# |
VB |
JScript |
|
WindowsPrincipal 类按 NT 组成员关系确定角色。应用程序若要确定自己的角色,可以在它们的 Global.asax 文件中处理 WindowsAuthentication_OnAuthenticate 事件并将自己实现 System.Security.Principal.IPrincipal 的类附加到请求,如下面的示例所示:
// Create a class that implements IPrincipal
public class MyPrincipal : IPrincipal {
// implement application-defined role mappings
}
// In a Global.asax file:
public void WindowsAuthentication_OnAuthenticate(Object Source, WindowsAuthenticationEventArgs e) {
// Attach a new application-defined class that implements IPrincipal to
// the request.
// Note that since IIS has already performed authentication, the provided
// identity is used.
e.User = new MyPrincipal(e.Identity);
}
' Create a class that implements IPrincipal
Public Class MyPrincipal : Inherits IPrincipal
' Implement application-defined role mappings
End Class
' In a Global.asax file
Public Sub WindowsAuthentication_OnAuthenticate(Source As Object, e As WindowsAuthenticationEventArgs)
' Attach a new application-defined class that implements IPrincipal to
' the request.
' Note that since IIS has already performed authentication, the provided
' identity is used.
e.User = New MyPrincipal(e.Identity)
End Sub
// Create a class that implements IPrincipal.
public class MyPrincipal implements IPrincipal {
// Implement application-defined role mappings.
}
// In a Global.asax file
public function WindowsAuthentication_OnAuthenticate(Source:Object, e:WindowsAuthenticationEventArgs) : void {
// Attach a new application-defined class that implements IPrincipal to
// the request.
// Note that since IIS has already performed authentication, the provided
// identity is used.
e.User = new MyPrincipal(e.Identity);
}
|
| C# |
VB |
JScript |
|