FormsAuthenticationModule.Authenticate Händelse
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.
Inträffar när programmet autentiserar den aktuella begäran.
public:
event System::Web::Security::FormsAuthenticationEventHandler ^ Authenticate;
public event System.Web.Security.FormsAuthenticationEventHandler Authenticate;
member this.Authenticate : System.Web.Security.FormsAuthenticationEventHandler
Public Custom Event Authenticate As FormsAuthenticationEventHandler
Händelsetyp
Exempel
I följande kodexempel används händelsen FormsAuthentication_OnAuthenticate för att ange User egenskapen för den aktuella HttpContext till ett GenericPrincipal objekt som har en anpassad Identity.
public void FormsAuthentication_OnAuthenticate(object sender, FormsAuthenticationEventArgs args)
{
if (FormsAuthentication.CookiesSupported)
{
if (Request.Cookies[FormsAuthentication.FormsCookieName] != null)
{
try
{
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(
Request.Cookies[FormsAuthentication.FormsCookieName].Value);
args.User = new System.Security.Principal.GenericPrincipal(
new Samples.AspNet.Security.MyFormsIdentity(ticket),
new string[0]);
}
catch (Exception e)
{
// Decrypt method failed.
}
}
}
else
{
throw new HttpException("Cookieless Forms Authentication is not " +
"supported for this application.");
}
}
Public Sub FormsAuthentication_OnAuthenticate(sender As Object, _
args As FormsAuthenticationEventArgs)
If FormsAuthentication.CookiesSupported Then
If Not Request.Cookies(FormsAuthentication.FormsCookieName) Is Nothing Then
Try
Dim ticket As FormsAuthenticationTicket = FormsAuthentication.Decrypt( _
Request.Cookies(FormsAuthentication.FormsCookieName).Value)
args.User = New System.Security.Principal.GenericPrincipal( _
New Samples.AspNet.Security.MyFormsIdentity(ticket), _
New String(0) {})
Catch e As HttpException
' Decrypt method failed.
End Try
End If
Else
Throw New Exception("Cookieless Forms Authentication is not " & _
"supported for this application.")
End If
End Sub
Kommentarer
Händelsen Authenticate utlöses under AuthenticateRequest händelsen.
Du kan hantera händelsen Authenticate för klassen FormsAuthenticationModule genom att ange en underrutin med namnet FormsAuthentication_OnAuthenticate i filen Global.asax för ditt ASP.NET-program.
Du kan använda egenskapen FormsAuthenticationEventArgsUser som angavs för händelsen FormsAuthentication_OnAuthenticate för att ange User egenskapen för den aktuella HttpContext till ett anpassat IPrincipal objekt. Om du inte anger något värde för User egenskapen under händelsen FormsAuthentication_OnAuthenticate används identiteten som tillhandahålls av formulärautentiseringsbiljetten i cookien eller URL:en.
Händelsen FormsAuthentication_OnAuthenticate utlöses endast när autentiseringsläget är inställt på Forms i autentiseringselementet (ASP.NET inställningsschema) elementet i programmets konfigurationsfil och FormsAuthenticationModule är en aktiv HTTP-modul för programmet.