FormsAuthenticationEventArgs.User Properti
Definisi
Penting
Beberapa informasi terkait produk prarilis yang dapat diubah secara signifikan sebelum dirilis. Microsoft tidak memberikan jaminan, tersirat maupun tersurat, sehubungan dengan informasi yang diberikan di sini.
Mendapatkan atau mengatur objek yang IPrincipal akan dikaitkan dengan permintaan saat ini.
public:
property System::Security::Principal::IPrincipal ^ User { System::Security::Principal::IPrincipal ^ get(); void set(System::Security::Principal::IPrincipal ^ value); };
public System.Security.Principal.IPrincipal User { get; set; }
member this.User : System.Security.Principal.IPrincipal with get, set
Public Property User As IPrincipal
Nilai Properti
Objek IPrincipal yang akan dikaitkan dengan permintaan saat ini.
Contoh
Contoh berikut menggunakan peristiwa FormsAuthentication_OnAuthenticate untuk mengatur User properti saat ini HttpContext ke GenericPrincipal objek dengan kustom 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
Keterangan
Anda dapat menggunakan User properti untuk mengatur User properti saat ini HttpContext ke objek kustom IPrincipal .
Jika Anda tidak menentukan nilai untuk User properti selama peristiwa FormsAuthentication_OnAuthenticate , identitas yang disediakan oleh tiket autentikasi formulir di cookie atau URL akan digunakan.