2

フォーム認証を使用してサイトで認証し、セッション Cookie と共にユーザーをそのサイトにリダイレクトする必要があります。これを成功させる方法がわかりません。これまでのコードは次のとおりです..まだそのアプリのログインページにリダイレクトされます。どんな助けでも大歓迎です!


protected void Button1_Click(object sender, EventArgs e)
{
 string data = "nickname=&login={0}&password={1}&action_login.x=70&action_login.y=14action_login=Login";
 string postdata = String.Format(data, "test", "test");
 string page = @"http://1.1.1.1/home.asp";
 string loginPage = @"http://1.1.1.1/auth.asp";
 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(loginPage);
 request.Method = "POST";
 request.ContentType = "application/x-www-form-urlencoded";
 request.AllowAutoRedirect = false;
 ASCIIEncoding encoding = new ASCIIEncoding(); //encoder
 byte[] requestData = encoding.GetBytes(postdata); //encode post data
 request.ContentLength = requestData.Length;
 //write the post data to the request
 Stream requestStream = request.GetRequestStream();
 // Send the data.
 requestStream.Write(requestData, 0, requestData.Length);
 requestStream.Close();
 try
 {
  HttpWebResponse response = (HttpWebResponse) request.GetResponse();
  string cookieHeader = response.GetResponseHeader("Set-Cookie");
  string cookieValue = cookieHeader.Replace("pp_session_id=", "");
  HttpCookie cookie = new HttpCookie("pp_session_id",cookieValue);
  cookie.Domain = "1.1.1.1";
  cookie.Path = "/";
  Response.Clear();
  Response.StatusCode = 302;
  //Response.AddHeader("Set-Cookie", cookieHeader);
  Response.AddHeader("Location",page);
  Response.RedirectLocation = page;
  Response.Cookies.Add(cookie);
  Response.Flush();

 }
 catch (WebException ex)
 {
  Response.Write(ex.Message);
 }
}
4

3 に答える 3

1

Mozilla Firefox で Firebug を使用して、Web アプリケーションにログインするときにブラウザーが正確に何をするかを確認します。次に、コードを使用して同じシーケンスをシミュレートします。

または、wireshark を使用して、ブラウザーから送信された要求を盗聴することもできます。

コードからわかることの 1 つは、Cookie を明示的に追加していることです。あなたはこれをすべきではありません。そのサイトへのすべてのリクエストで Cookie が送信されるように、リクエストに CookieContainer を設定する必要があります。

それが役立つことを願っています。

于 2009-09-17T01:29:05.793 に答える
1

FormsAuthenticationクラスを使用することの何が問題になっていますか? 特に、次のシーケンス (またはそのバリエーション) を試しましたか?

FormsAuthentication.Authenticate();

FormsAuthentication.SetAuthCookie();

FormsAuthentication.RedirectFromLoginPage();

于 2009-09-17T01:12:13.733 に答える
0

リモート Web アプリの認証済みページにリクエストを送信する必要があると思います。

有効なセッションができるように、それが提供する Cookie を取得する必要があります。aspnet セッション ID は Cookie で渡されます。次に、そのアプリに必要なユーザー名とパスワードを、受け取った Cookie と共に渡す必要があります。これにより、有効な認証済みセッションが得られます。

于 2009-12-03T04:58:21.707 に答える