Authentification Windows
Si vous utilisez l'authentification Windows ASP.NET, ASP.NET attache un objet WindowsPrincipal à la demande en cours. Cet objet est utilisé par l'autorisation d'URL. L'application peut également l'employer par programme afin de déterminer si une identité à l'origine d'une demande joue un rôle donné.
if(User.IsInRole("Administrators")) {
DisplayPrivilegedContent();
}
If User.IsInRole("Administrators") Then
DisplayPrivilegedContent()
End If
if(User.IsInRole("Administrators")) {
DisplayPrivilegedContent();
}
|
|
C#
|
VB
|
JScript
|
|
La classe WindowsPrincipal détermine les rôles par l'appartenance aux groupes NT. Les applications qui souhaitent déterminer leurs propres rôles doivent gérer l'événement WindowsAuthentication_OnAuthenticate dans leur fichier Global.asax et attacher à la demande leur propre classe implémentant System.Security.Principal.IPrincipal, comme le montre l'exemple suivant :
// 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
|
|
L'exemple suivant illustre l'accès au nom d'un utilisateur authentifié, disponible en tant que User.Identity.Name. Les programmeurs familiarisés avec ASP peuvent constater que cette valeur est toujours disponible sous la forme de la variable serveur AUTH_USER :
|