FormsAuthenticationModule.Authenticate Evento
Definição
Importante
Algumas informações dizem respeito a um produto pré-lançado que pode ser substancialmente modificado antes de ser lançado. A Microsoft não faz garantias, de forma expressa ou implícita, em relação à informação aqui apresentada.
Ocorre quando a aplicação autentica o pedido atual.
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
Tipo de Evento
Exemplos
O seguinte exemplo de código usa o evento FormsAuthentication_OnAuthenticate para definir a User propriedade do atual HttpContext para um GenericPrincipal objeto que tem um custom 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
Observações
O Authenticate evento é levantado durante o AuthenticateRequest evento.
Pode gerir o evento Authenticate da classe FormsAuthenticationModule especificando uma sub-rotina chamada FormsAuthentication_OnAuthenticate no ficheiro Global.asax da sua aplicação ASP.NET.
Podes usar a FormsAuthenticationEventArgsUser propriedade fornecida ao evento FormsAuthentication_OnAuthenticate para definir a User propriedade do atual HttpContext para um objeto personalizado IPrincipal . Se não especificar um valor para a User propriedade durante o evento FormsAuthentication_OnAuthenticate , a identidade fornecida pelo ticket de autenticação dos formulários no cookie ou URL é utilizada.
O evento FormsAuthentication_OnAuthenticate só é ativado quando o modo de autenticação está definido para Forms no elemento autenticação (ASP.NET Settings Schema) do ficheiro de configuração da aplicação e o FormsAuthenticationModule é um módulo HTTP ativo para a aplicação.