HttpCookie 클래스

정의

개별 HTTP 쿠키를 만들고 조작할 수 있는 형식이 안전한 방법을 제공합니다.

public ref class HttpCookie sealed
public sealed class HttpCookie
type HttpCookie = class
Public NotInheritable Class HttpCookie
상속
HttpCookie

예제

다음 코드 예제에서는 개체에 명명 DateCookieExample HttpRequest 된 쿠키를 확인 하는 방법을 보여 줍니다. 쿠키를 찾을 수 없으면 만들어지고 개체에 HttpResponse 추가됩니다. 쿠키는 10분 후에 만료되도록 설정됩니다.

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    protected void Page_Load(object sender, EventArgs e)
    {
        StringBuilder sb = new StringBuilder();
        // Get cookie from the current request.
        HttpCookie cookie = Request.Cookies.Get("DateCookieExample");
        
        // Check if cookie exists in the current request.
        if (cookie == null)
        {
            sb.Append("Cookie was not received from the client. ");
            sb.Append("Creating cookie to add to the response. <br/>");
            // Create cookie.
            cookie = new HttpCookie("DateCookieExample");
            // Set value of cookie to current date time.
            cookie.Value = DateTime.Now.ToString();
            // Set cookie to expire in 10 minutes.
            cookie.Expires = DateTime.Now.AddMinutes(10d);
            // Insert the cookie in the current HttpResponse.
            Response.Cookies.Add(cookie);
        }
        else
        {
            sb.Append("Cookie retrieved from client. <br/>");
            sb.Append("Cookie Name: " + cookie.Name + "<br/>");
            sb.Append("Cookie Value: " + cookie.Value + "<br/>");
            sb.Append("Cookie Expiration Date: " + 
                cookie.Expires.ToString() + "<br/>");
        }
        Label1.Text = sb.ToString();
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>HttpCookie Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:Label id="Label1" runat="server"></asp:Label>
    </div>
    </form>
</body>
</html>
<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim sb As New StringBuilder()
        ' Get cookie from current request.
        Dim cookie As HttpCookie
        cookie = Request.Cookies.Get("DateCookieExample")
        
        ' Check if cookie exists in the current request
        If (cookie Is Nothing) Then
            sb.Append("Cookie was not received from the client. ")
            sb.Append("Creating cookie to add to the response. <br/>")
            ' Create cookie.
            cookie = New HttpCookie("DateCookieExample")
            ' Set value of cookie to current date time.
            cookie.Value = DateTime.Now.ToString()
            ' Set cookie to expire in 10 minutes.
            cookie.Expires = DateTime.Now.AddMinutes(10D)
            ' Insert the cookie in the current HttpResponse.
            Response.Cookies.Add(cookie)
        Else
            sb.Append("Cookie retrieved from client. <br/>")
            sb.Append("Cookie Name: " + cookie.Name + "<br/>")
            sb.Append("Cookie Value: " + cookie.Value + "<br/>")
            sb.Append("Cookie Expiration Date: " & _
                cookie.Expires.ToString() & "<br/>")
        End If
        Label1.Text = sb.ToString()

    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>HttpCookie Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:Label id="Label1" runat="server"></asp:Label>    
    </div>
    </form>
</body>
</html>

설명

클래스는 HttpCookie 개별 쿠키의 속성을 가져오고 설정합니다. 클래스는 HttpCookieCollection 여러 쿠키를 저장, 검색 및 관리하는 메서드를 제공합니다.

ASP.NET 두 내장 쿠키 컬렉션을 포함합니다. 개체의 HttpRequest 컬렉션을 통해 Cookies 액세스되는 컬렉션에는 클라이언트가 헤더의 서버로 전송하는 쿠키가 Cookie 포함되어 있습니다. 개체 컬렉션을 HttpResponse 통해 Cookies 액세스되는 컬렉션에는 서버에서 만들어지고 HTTP 응답 헤더의 클라이언트로 전송되는 새 쿠키가 Set-Cookie 포함됩니다.

생성자

HttpCookie(String)

새 쿠키를 만들고 이름을 지정합니다.

HttpCookie(String, String)

새 쿠키를 만들어 이름을 지정하고 값을 할당합니다.

속성

Domain

쿠키를 연결할 도메인을 가져오거나 설정합니다.

Expires

쿠키의 만료 날짜 및 시간을 가져오거나 설정합니다.

HasKeys

쿠키에 하위 키가 있는지 여부를 나타내는 값을 가져옵니다.

HttpOnly

클라이언트측 스크립트를 사용하여 쿠키에 액세스할 수 있는지 여부를 지정하는 값을 가져오거나 설정합니다.

Item[String]

Values 속성에 대한 바로 가기를 가져옵니다. 이 속성은 이전 버전의 ASP(Active Server Pages)와의 호환성을 위해 제공됩니다.

Name

쿠키의 이름을 가져오거나 설정합니다.

Path

현재 쿠키와 함께 전송할 가상 경로를 가져오거나 설정합니다.

SameSite

쿠키의 SameSite 특성에 대한 값을 가져오거나 설정합니다.

Secure

SSL(Secure Sockets Layer)을 사용하여 쿠키를 전송할지, 즉 HTTPS로만 쿠키를 전송할지 여부를 나타내는 값을 가져오거나 설정합니다.

Shareable

쿠키가 출력 캐시에 포함될 수 있는지 결정합니다.

Value

개별 쿠키 값을 가져오거나 설정합니다.

Values

쿠키 개체 하나에 들어 있는 키/값 쌍의 컬렉션을 가져옵니다.

메서드

Equals(Object)

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

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

기본 해시 함수로 작동합니다.

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

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

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

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

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

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

(다음에서 상속됨 Object)
TryParse(String, HttpCookie)

쿠키에 대한 지정된 문자열 표현을 해당 HttpCookie 요소로 변환하고, 변환에 성공했는지를 나타내는 값을 반환합니다.

적용 대상

추가 정보