AuthenticationService.CreatingCookie Kejadian
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.
Terjadi ketika cookie autentikasi sedang diatur.
public:
static event EventHandler<System::Web::ApplicationServices::CreatingCookieEventArgs ^> ^ CreatingCookie;
public static event EventHandler<System.Web.ApplicationServices.CreatingCookieEventArgs> CreatingCookie;
member this.CreatingCookie : EventHandler<System.Web.ApplicationServices.CreatingCookieEventArgs>
Public Shared Custom Event CreatingCookie As EventHandler(Of CreatingCookieEventArgs)
Jenis Acara
Contoh
Contoh berikut menunjukkan cara mengikat penanganan aktivitas ke CreatingCookie peristiwa dalam Application_Start
metode file Global.asax.
void Application_Start(object sender, EventArgs e)
{
System.Web.ApplicationServices.AuthenticationService.CreatingCookie
+= new EventHandler<System.Web.ApplicationServices.CreatingCookieEventArgs>
(AuthenticationService_CreatingCookie);
}
Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
AddHandler System.Web.ApplicationServices.AuthenticationService.CreatingCookie, _
AddressOf Me.AuthenticationService_CreatingCookie
End Sub
Contoh berikut menunjukkan penanganan aktivitas untuk CreatingCookie peristiwa dalam file Global.asax. Penanganan aktivitas menyesuaikan cookie autentikasi dengan menambahkan nilai dalam CustomCredential properti ke UserData properti . CustomCredential Simpan properti dalam cookie hanya jika Anda tahu bahwa data dalam properti tidak sensitif. Pengguna berbahaya dapat mengakses nilai dalam cookie.
void AuthenticationService_CreatingCookie(object sender,
System.Web.ApplicationServices.CreatingCookieEventArgs e)
{
FormsAuthenticationTicket ticket = new
FormsAuthenticationTicket
(1,
e.UserName,
DateTime.Now,
DateTime.Now.AddMinutes(30),
e.IsPersistent,
e.CustomCredential,
FormsAuthentication.FormsCookiePath);
string encryptedTicket =
FormsAuthentication.Encrypt(ticket);
HttpCookie cookie = new HttpCookie
(FormsAuthentication.FormsCookieName,
encryptedTicket);
cookie.Expires = DateTime.Now.AddMinutes(30);
HttpContext.Current.Response.Cookies.Add(cookie);
e.CookieIsSet = true;
}
Sub AuthenticationService_CreatingCookie(ByVal sender As Object, _
ByVal e As System.Web.ApplicationServices.CreatingCookieEventArgs)
Dim ticket As FormsAuthenticationTicket = New _
FormsAuthenticationTicket _
(1, _
e.Username, _
DateTime.Now, _
DateTime.Now.AddMinutes(30), _
e.IsPersistent, _
e.CustomCredential, _
FormsAuthentication.FormsCookiePath)
Dim encryptedTicket As String = FormsAuthentication.Encrypt(ticket)
Dim cookie As HttpCookie = New _
HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket)
cookie.Expires = DateTime.Now.AddMinutes(30)
HttpContext.Current.Response.Cookies.Add(cookie)
e.CookieIsSet = True
End Sub
Keterangan
Peristiwa CreatingCookie dimunculkan ketika cookie autentikasi sedang diatur setelah kredensial pengguna divalidasi. Buat penanganan aktivitas untuk CreatingCookie peristiwa guna menyesuaikan cookie autentikasi.
Berlaku untuk
Lihat juga
Saran dan Komentar
https://aka.ms/ContentUserFeedback.
Segera hadir: Sepanjang tahun 2024 kami akan menghentikan penggunaan GitHub Issues sebagai mekanisme umpan balik untuk konten dan menggantinya dengan sistem umpan balik baru. Untuk mengetahui informasi selengkapnya, lihat:Kirim dan lihat umpan balik untuk