C를 사용한 샘플 ASP.NET 쿠키로 샤프 리디렉션

  • 릴리스 버전: Yokohama
  • 업데이트 날짜 2025년 01월 30일
  • 읽기3분
  • 이 샘플 ASP .NET 코드는 간단한 인증 포털을 만들고 암호화되지 않은 HTTP 헤더를 쿠키로 전달합니다.

    주:
    여기에 설명된 기능을 사용하려면 관리자 역할이 필요합니다.
    주:
    쿠키는 도메인별로 다르며 다른 네트워크 도메인에서 사용할 수 없습니다. 쿠키를 읽을 수 있는 유일한 도메인은 쿠키를 설정하는 도메인입니다. 어떤 도메인 이름을 설정하든 상관없습니다. SSO 포털이 인스턴스 ServiceNow 와 동일한 네트워크 도메인에 있도록 하는 옵션이 없는 경우(예: 온 프레미스 배포에서 대안은 URL GET 또는 POST 매개변수를 사용하여 SSO 토큰을 전달하는 것입니다.

    이 샘플에서는 다음을 가정합니다.

    • 웹 서버는 ASP .NET 및 C를 지원합니다.#
    • 대상 ServiceNow 인스턴스는 https://<instance name>.service-now.com/ 입니다.
    • SiteMinder 또는 다른 1회 사용자 인증(SSO) 애플리케이션이 사용자를 사전 인증했습니다.
    • 대상 ServiceNow 인스턴스에는 SM_USER의 HTTP 헤더가 필요합니다.

    ASP 코드를 변경하여 사용자를 적절한 ServiceNow 인스턴스로 리디렉션합니다.

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Portal Page Login</title>
    <%--    <meta http-equiv="REFRESH" content="0;url=https://<instance name>.service-now.com/">--%>
     
    </head>
    <body>
        <form id="form1" runat="server">
        <h2><b> Portal Page Login </b></h2>
            <hr style="position: static" />
             <br />
             <asp:Label ID="Label2" runat="server" Font-Size="Larger" Height="21px" Style="position: static"
                Text="Instance URL:" Width="113px"></asp:Label> 
            <asp:TextBox ID="urlBox" runat="server" Font-Size="Large" Style="position: static"></asp:TextBox><br />
            <br />
             <asp:Label ID="Label1" runat="server" Font-Size="Larger" Height="17px" Style="position: static;" Text="User Id:" Width="113px"></asp:Label> 
            <asp:TextBox ID="userNameBox" runat="server" Font-Size="Large" Style="position: static;"></asp:TextBox>
     
            <br />
            <br />
            <asp:Button ID="Button1" runat="server" Height="39px" Style="position: static;" Text="Ok"
                Width="88px" OnClick="Button1_click" />
        </form>
    </body>
    </html>
    </asp>
    그림 1. ASP 포털 리디렉션

    다음 C# 코드는 폼에 대한 OnClick 단추 이벤트를 처리합니다. 코드는 다음과 같습니다.

    • 쿠키 "SM_USER" 작성
    • ASP 양식에 지정된 URL로 리디렉션을 수행합니다.

    C# 코드를 변경하여 적절한 쿠키 이름을 만듭니다.

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
     
    public partial class _Default : System.Web.UI.Page 
    {
        protected void Page_Load(object sender, EventArgs e)
        {
     
        }
        protected void Button1_click(object sender, EventArgs e)
        {
            try
            {
                HttpCookie myCookie = new HttpCookie("SM_USER");
                myCookie.Value = userNameBox.Text;
                Response.Cookies.Add(myCookie);
                Response.Redirect(urlBox.Text);
     
            }catch {} 
        }
    }