cookie を使用した C Sharp リダイレクトによるサンプル ASP.NET

  • リリースバージョン: Zurich
  • 更新日 2025年07月31日
  • 所要時間:4分
  • このサンプル ASP.NET コードは、簡易認証ポータルを作成し、暗号化されていない HTTP ヘッダーを cookie として渡します。

    注:
    ここで説明する機能には、admin ロールが必要です。
    注:
    cookie はドメイン固有であり、異なるネットワークドメイン間で使用することはできません。cookie を読み取ることができる唯一のドメインは、それを設定するドメインです。どのようなドメイン名を設定するかは関係ありません。SSO ポータルを ServiceNow インスタンスと同じネットワークドメインに置くことを選択できない場合 (たとえば、オンプレミス展開の場合)、代わりに URL GET または POST パラメーターを使用して SSO トークンを渡します。

    この例では、以下を前提としています。

    • Web サーバーは ASP.NET と C をサポートします#
    • ターゲットServiceNowインスタンスは https://<instance name>.service-now.com/
    • SiteMinder または別のシングルサインオンアプリケーションがユーザーを事前認証している
    • ターゲット ServiceNow インスタンスは HTTP ヘッダー SM_USER を予期している

    ユーザーを適切な ServiceNow インスタンスにリダイレクトするように ASP コードを変更します。

    <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 ボタンイベントを処理します。コード:

    • cookie「SM_USER」を作成します
    • ASP フォームで指定された URL へのリダイレクトを実行します。

    C# コードを変更して、適切な cookie 名を作成します。

    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 {} 
        }
    }