CreatingCookieEventArgs.UserName Proprietà

Definizione

Ottiene il nome per l'utente.

public:
 property System::String ^ UserName { System::String ^ get(); };
public string UserName { get; }
member this.UserName : string
Public ReadOnly Property UserName As String

Valore della proprietà

Nome fornito dall'utente per l'autenticazione.

Esempio

Nell'esempio seguente viene illustrato un gestore eventi per l'evento CreatingCookie . Il gestore recupera i valori utente dall'oggetto CreatingCookieEventArgs per personalizzare il cookie di autenticazione. La Name proprietà viene impostata sui valori della UserName proprietà .

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

Commenti

Usare la UserName proprietà per recuperare il nome di autenticazione quando si impostano valori personalizzati nel ticket di autenticazione. In genere, si imposta la Name proprietà dell'oggetto FormsAuthenticationTicket sul UserName valore .

Si applica a