我试图实现以下目标:
我设法实现了部分功能,但我仍然坚持显示重定向返回。谁知道这是否可以实现?
以下是在WebsiteA应用中调用的一些代码:
[HttpPost]
[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
public ActionResult LogIn(myModel model)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://websiteB/LogIn.aspx");
request.AllowAutoRedirect = true;
request.Method = "POST";
request.Headers.Add("MyUserToLogon", model.User); //I'm sending my user through headers
string postData = "This is a test";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = byteArray.Length;
Stream dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
//Get the response from the Login Page
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
// Display the status.
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
dataStream = response.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
string responseFromServer = reader.ReadToEnd();
Console.WriteLine(responseFromServer);
reader.Close();
dataStream.Close();
response.Close();
//What to do here? I want to go to the redirected page from WebSiteB/Page1.aspx
//See code below for WebSiteB.
return null;
}
在WebSiteB中,我有代码在用户成功登录时进行重定向:
.......
users = Request.QueryString["MyUserToLogon"];
.......
private void LogIn(string user)
{
GrantUser(user, Session.SessionID);
HttpCookie mycookie = new HttpCookie("test");
mycookie .Expires = DateTime.Now.AddMinutes(10);
Response.Cookies.Add(mycookie);
Response.AddHeader("mycookie ", mycookie .Value);
Response.Redirect("WebsiteB/Page1.aspx");
}
任何帮助都会非常感激。
答案 0 :(得分:0)
我的网站遇到了同样的问题。
您无法使用重定向,因为它是由IIS完成的,您永远不会从WebSiteB收回所有身份验证Cookie。
在我的情况下,返回视图而不是重定向工作。然后在设置cookie后重定向WebSiteA
您似乎还需要为请求添加cookie容器以获取cookie。
这可能是代码
登陆网站
[HttpPost]
public ActionResult Login(string accountId)
{
var url = "http://...."
var request = (HttpWebRequest)WebRequest.Create(url + "/Account/WamLogin/");
request.Headers.Add("user", accountId);
request.CookieContainer = new CookieContainer();
var response = (HttpWebResponse)request.GetResponse();
foreach (Cookie cook in response.Cookies)
{
Response.Cookies.Add(new HttpCookie(cook.Name, cook.Value) { Expires = cook.Expires, HttpOnly = cook.HttpOnly, Domain = cook.Domain });
}
return new RedirectResult(url);
}
这是WebSiteB代码
[AllowAnonymous]
public virtual ActionResult WamLogin(string returnUrl, string id)
{
var accountId= Request.Headers["user"];
.....
return View(MVC.Account.Views.Login);
}
希望这会对你有所帮助