다음을 통해 공유


FormsAuthenticationTicket 클래스

정의

사용자를 식별하기 위해 양식 인증에 사용되는 티켓의 속성 및 값에 대한 액세스를 제공합니다. 이 클래스는 상속할 수 없습니다.

public ref class FormsAuthenticationTicket sealed
[System.Serializable]
public sealed class FormsAuthenticationTicket
[<System.Serializable>]
type FormsAuthenticationTicket = class
Public NotInheritable Class FormsAuthenticationTicket
상속
FormsAuthenticationTicket
특성

예제

다음 코드 예제에서는 메서드의 Encrypt 결과를 사용하여 FormsCookieName 쿠키에 저장하고 사용자를 메서드에서 GetRedirectUrl 반환된 URL로 리디렉션합니다.

중요합니다

이 예제에는 잠재적인 보안 위협인 사용자 입력을 허용하는 텍스트 상자가 포함되어 있습니다. 기본적으로 ASP.NET 웹 페이지는 사용자 입력에 스크립트 또는 HTML 요소가 포함되지 않는지 확인합니다. 자세한 내용은 스크립트 악용 개요를 참조하세요.

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Security" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  private void Login_Click(Object sender, EventArgs e)
  {
    // Create a custom FormsAuthenticationTicket containing
    // application specific data for the user.

    string username     = UserNameTextBox.Text;
    string password     = UserPassTextBox.Text;
    bool   isPersistent = false;

    if (Membership.ValidateUser(username, password))
    {
      string userData = "ApplicationSpecific data for this user.";

      FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,
        username,
        DateTime.Now,
        DateTime.Now.AddMinutes(30),
        isPersistent,
        userData,
        FormsAuthentication.FormsCookiePath);

      // Encrypt the ticket.
      string encTicket = FormsAuthentication.Encrypt(ticket);

      // Create the cookie.
      Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));

      // Redirect back to original URL.
      Response.Redirect(FormsAuthentication.GetRedirectUrl(username, isPersistent));
    }
    else
    {
      Msg.Text = "Login failed. Please check your user name and password and try again.";
    }
  }

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Forms Authentication Login</title>
</head>
<body>
    <form id="form1" runat="server">
        <span style="BACKGROUND: #80ff80; font-weight:bold"> 
            Login Page
        </span> 
        <asp:Label id="Msg" ForeColor="maroon" runat="server" /><br />
        <table border="0">
            <tbody>
                <tr>
                    <td>Username:</td>
                    <td><asp:TextBox id="UserNameTextBox" runat="server" /></td>
                    <td>
                      <asp:RequiredFieldValidator id="RequiredFieldValidator1" 
                                                  runat="server" ErrorMessage="*" 
                                                  Display="Static" 
                                                  ControlToValidate="UserNameTextBox" />
                    </td>
                </tr>
                <tr>
                    <td>Password:</td>
                    <td><asp:TextBox id="UserPassTextBox" TextMode="Password" runat="server" /></td>
                    <td>
                      <asp:RequiredFieldValidator id="RequiredFieldValidator2" 
                                                  runat="server" ErrorMessage="*" 
                                                  Display="Static" 
                                                  ControlToValidate="UserPassTextBox" />
                    </td>
                </tr>
            </tbody>
        </table>
        <input type="submit" value="Login" runat="server" onserverclick="Login_Click" />
    </form>
</body>
</html>
<%@ Page Language="VB" %>
<%@ Import Namespace="System.Web.Security" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  Private Sub Login_Click(sender As Object, e As EventArgs)
  
    ' Create a custom FormsAuthenticationTicket containing
    ' application specific data for the user.

        Dim username As String = UserNameTextBox.Text
        Dim password As String = UserPassTextBox.Text
        Dim isPersistent As Boolean = False

    If Membership.ValidateUser(username, password) Then
    
      Dim userData As String = "ApplicationSpecific data for this user."

      Dim ticket As FormsAuthenticationTicket = New FormsAuthenticationTicket(1, _
        username, _
        DateTime.Now, _
        DateTime.Now.AddMinutes(30), _
        isPersistent, _
        userData, _
        FormsAuthentication.FormsCookiePath)

      ' Encrypt the ticket.
      Dim encTicket As String = FormsAuthentication.Encrypt(ticket)

      ' Create the cookie.
      Response.Cookies.Add(New HttpCookie(FormsAuthentication.FormsCookieName, encTicket))

      ' Redirect back to original URL.
      Response.Redirect(FormsAuthentication.GetRedirectUrl(username, isPersistent))
    Else    
      Msg.Text = "Login failed. Please check your user name and password and try again."
    End If
  End Sub

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Forms Authentication Login</title>
</head>
<body>
    <form id="form1" runat="server">
        <span style="BACKGROUND:#80ff80; font-weight:bold"> 
          Login Page
        </span> 
        <asp:Label id="Msg" ForeColor="maroon" runat="server" /><br />
        <table border="0">
            <tbody>
                <tr>
                    <td>Username:</td>
                    <td><asp:TextBox id="UserNameTextBox" runat="server" /></td>
                    <td>
                      <asp:RequiredFieldValidator id="RequiredFieldValidator1" 
                                                  runat="server" ErrorMessage="*" 
                                                  Display="Static" 
                                                  ControlToValidate="UserNameTextBox" />
                    </td>
                </tr>
                <tr>
                    <td>Password:</td>
                    <td><asp:TextBox id="UserPassTextBox" TextMode="Password" runat="server" /></td>
                    <td>
                      <asp:RequiredFieldValidator id="RequiredFieldValidator2" 
                                                  runat="server" ErrorMessage="*" 
                                                  Display="Static" 
                                                  ControlToValidate="UserPassTextBox" />
                    </td>
                </tr>
            </tbody>
        </table>
        <input type="submit" value="Login" runat="server" onserverclick="Login_Click" />
    </form>
</body>
</html>

설명

FormsAuthenticationTicket 클래스는 인증된 사용자를 식별하기 위해 양식 인증에 사용되는 인증 티켓을 나타내는 개체를 만드는 데 사용됩니다. 양식 인증 티켓의 속성과 값은 쿠키 또는 URL에 저장된 암호화된 문자열로 변환됩니다.

클래스는 FormsAuthentication 쿠키 또는 URL에서 저장할 수 있는 문자열 값을 만드는 메서드를 FormsAuthenticationTicket제공합니다Encrypt. 또한 이 클래스는 FormsAuthentication 양식 인증 쿠키 또는 URL에서 검색된 암호화된 인증 티켓에서 개체를 만드는 FormsAuthenticationTicket 메서드를 제공합니다Decrypt.

FormsAuthenticationTicket 현재 인증된 사용자의 경우 클래스의 FormsIdentity 속성을 사용하여 Ticket 액세스할 수 있습니다. 현재 FormsIdentity 속성을 형식으로 캐스팅 Identity 하여 현재 User 개체에 액세스할 수 있습니다 FormsIdentity.

생성자

Name Description
FormsAuthenticationTicket(Int32, String, DateTime, DateTime, Boolean, String, String)

쿠키 이름, 버전, 디렉터리 경로, 발급 날짜, 만료 날짜, 지속성 및 사용자 정의 데이터를 사용하여 클래스의 FormsAuthenticationTicket 새 인스턴스를 초기화합니다.

FormsAuthenticationTicket(Int32, String, DateTime, DateTime, Boolean, String)

쿠키 이름, 버전, 만료 날짜, 발급 날짜, 지속성 및 사용자별 데이터를 사용하여 클래스의 FormsAuthenticationTicket 새 인스턴스를 초기화합니다. 쿠키 경로는 애플리케이션의 구성 파일에 설정된 기본값으로 설정됩니다.

FormsAuthenticationTicket(String, Boolean, Int32)

쿠키 이름 및 만료 정보를 사용하여 클래스의 FormsAuthenticationTicket 새 인스턴스를 초기화합니다.

속성

Name Description
CookiePath

양식 인증 티켓의 쿠키 경로를 가져옵니다.

Expiration

양식 인증 티켓이 만료되는 현지 날짜 및 시간을 가져옵니다.

Expired

양식 인증 티켓이 만료되었는지 여부를 나타내는 값을 가져옵니다.

IsPersistent

양식 인증 티켓 정보를 포함하는 쿠키가 영구적인지 여부를 나타내는 값을 가져옵니다.

IssueDate

양식 인증 티켓이 원래 발급된 현지 날짜 및 시간을 가져옵니다.

Name

양식 인증 티켓과 연결된 사용자 이름을 가져옵니다.

UserData

티켓과 함께 저장된 사용자별 문자열을 가져옵니다.

Version

티켓의 버전 번호를 가져옵니다.

메서드

Name Description
Equals(Object)

지정된 개체가 현재 개체와 같은지 여부를 확인합니다.

(다음에서 상속됨 Object)
GetHashCode()

기본 해시 함수로 사용됩니다.

(다음에서 상속됨 Object)
GetType()

현재 인스턴스의 Type 가져옵니다.

(다음에서 상속됨 Object)
MemberwiseClone()

현재 Object단순 복사본을 만듭니다.

(다음에서 상속됨 Object)
ToString()

현재 개체를 나타내는 문자열을 반환합니다.

(다음에서 상속됨 Object)

적용 대상

추가 정보