C#-HttpWebResponse重定向到外部URL [英] C# - HttpWebResponse redirect to external URL

查看:680
本文介绍了C#-HttpWebResponse重定向到外部URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力实现以下目标:

I'm trying to achieve the following:


  • 我正在建立一个MVC网站,该网站将帮助我自动登录到另一个网站

  • 网站A 将使用HttpWebRequest调用网站B

  • 网站A 将通过标头(request.headers.add)发送用户的详细信息

  • 网站B 将处理所有用户身份验证,并在内部将其重定向到

  • I'm building an MVC website that will help me to automatically logon to another site.
  • WebsiteA will call WebsiteB using HttpWebRequest.
  • WebsiteA will send the details of the user through headers (request.headers.add)
  • WebsiteB will handle all the user authentication and internally it will redirect to another page granting access to that user.

我设法实现了其中的一部分,但是我停留在显示重定向返回中。有人知道这是否可以实现吗?

I've managed to achieve part of it but I'm stuck in displaying the redirection return. Anyone knows if this can be achieved?

以下是在WebsiteA应用中被调用的一些代码:

Here is some code that gets called in the WebsiteA app:

        [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中,我有在用户成功登录后执行重定向的代码在:

In WebSiteB I have the code that does the redirection when the user is successfully logged in:

        .......
        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");
        }

任何帮助将不胜感激。

Any help would be really appreciated.

推荐答案

我的网站也遇到了同样的问题。

I had the same problem for my sites.

您不能使用重定向,因为它是由IIS完成的,您将永远不会从WebSiteB收到所有身份验证cookie。

You can't use redirect because it is done by IIS and you will never receive all your authentication cookies back from WebSiteB.

在我的情况下,返回视图而不是重定向有效。然后,在设置cookie之后,您可以在WebSiteA上进行重定向

In my case returning view instead of redirect worked. And then you redirect on WebSiteA after setting cookies

似乎您还需要添加cookie容器来获取返回cookie的请求。

Also seems you need to add cookie container for your request to get cookies back.

此处可能是代码

着陆点

 [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代码

Here is WebSiteB code

[AllowAnonymous]
public virtual ActionResult WamLogin(string returnUrl, string id)
{
    var accountId= Request.Headers["user"];

    .....

    return View(MVC.Account.Views.Login);
}

希望这对您有帮助

这篇关于C#-HttpWebResponse重定向到外部URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆