http location header, httpwebrequest/response.cookies.add ()를 사용하여 양식 인증을 사용하는 인증 된 페이지로 사용자를 리디렉션합니다.

StackOverflow https://stackoverflow.com/questions/1436252

문제

Forms 인증을 사용하여 사이트에서 인증 한 다음 세션 쿠키와 함께 사용자를 해당 사이트로 리디렉션해야합니다. 나는 이것을 성공적으로 수행하는 방법을 찾지 못했습니다. 지금까지 내 코드가 있습니다. 여전히 해당 앱 로그인 페이지로 리디렉션됩니다. 모든 도움이 감사합니다!


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);
 }
}
도움이 되었습니까?

해결책

Mozilla Firefox에서 Firebug를 사용하여 WebApp에 로그인 할 때 브라우저가 정확히 무엇을하는지 확인하십시오. 그런 다음 코드를 통해 동일한 시퀀스를 시뮬레이션합니다.

또는 Wireshark를 사용하여 브라우저에서 보낸 요청을 스니핑 할 수 있습니다.

코드에서 볼 수있는 한 가지는 쿠키를 명시 적으로 추가한다는 것입니다. 당신은 이것을하지 말아야합니다. 쿠키가 해당 사이트로 모든 요청을 보내도록 요청에 따라 CookieContainer를 설정해야합니다.

도움이되기를 바랍니다.

다른 팁

사용하는 데 무엇이 잘못 되었습니까? FormsAuthentication 수업? 특히 다음 순서 (또는 IT 변형)를 사용해 보셨습니까?

FormsAuthentication.Authenticate();

FormsAuthentication.SetAuthCookie();

FormsAuthentication.RedirectFromLoginPage();

원격 웹 앱에서 인증 된 페이지에 요청해야한다고 생각합니다.

유효한 세션이 있도록 쿠키를 가져와야합니다. ASPNET 세션 ID는 쿠키에 전달됩니다. 그런 다음받은 쿠키와 함께 해당 앱에 필요한 사용자 이름과 비밀번호를 전달하여 유효한 인증 세션을 가질 수 있습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top