CreatingCookieEventArgs 类

定义

CreatingCookie 事件提供数据。

public ref class CreatingCookieEventArgs : EventArgs
public class CreatingCookieEventArgs : EventArgs
type CreatingCookieEventArgs = class
    inherit EventArgs
Public Class CreatingCookieEventArgs
Inherits EventArgs
继承
CreatingCookieEventArgs

示例

以下示例演示事件的事件处理程序 CreatingCookie 。 处理程序从 CreatingCookieEventArgs 对象中检索用户值以自定义身份验证 Cookie。 处理程序将 属性的值 CustomCredential 保存在身份验证票证中,并将 属性设置为 CookieIsSettrue 以指示已创建身份验证 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

注解

CreatingCookieEventArgs 向任何 CreatingCookie 事件处理程序提供值。 通过使用此类的属性,可以在创建自定义身份验证 Cookie 时检索用户凭据。

AuthenticationServiceCreatingCookie 对用户凭据进行身份验证后设置身份验证 Cookie 时引发 事件。 类 AuthenticationService 构造 对象 CreatingCookieEventArgs 并将其传递给任何 CreatingCookie 事件处理程序。

通过 UserNamePasswordCustomCredential 属性检索用户凭据。 属性 CustomCredential 可以包含身份验证所需的任何自定义值,例如标识号。 属性 IsPersistent 指示身份验证 Cookie 是否应在当前会话后保留。 CookieIsSet如果创建身份验证 Cookie,请将 属性true设置为 ,以便 AuthenticationService 类不会创建 Cookie。

属性

CookieIsSet

获取或设置一个值,该值指示是否已创建身份验证 Cookie。

CustomCredential

获取用户提供的其他身份验证值。

IsPersistent

获取一个值,该值指示超出当前会话时是否应保留身份验证 Cookie。

Password

获取用户的密码。

UserName

获取用户的名称。

方法

Equals(Object)

确定指定对象是否等于当前对象。

(继承自 Object)
GetHashCode()

作为默认哈希函数。

(继承自 Object)
GetType()

获取当前实例的 Type

(继承自 Object)
MemberwiseClone()

创建当前 Object 的浅表副本。

(继承自 Object)
ToString()

返回表示当前对象的字符串。

(继承自 Object)

适用于

另请参阅