单一登录网站-MVC 3



我有两个网站,我使用相同的cookie登录,这很好。

我遇到的问题是,这两个网站在设计上完全不同,我希望其中一个网站处理登录功能,另一个网站只需向另一个发布用户名/密码,并在后端创建cookie。

我有这个功能,但问题是,在没有实际前往其他网站完成登录过程的情况下,我如何通知其他网站登录成功或失败?

我已经创建了这样的东西,效果很好,但困扰我的是我应该如何处理其他网站上的帖子,以及我应该返回什么作为回应

任何想法或替代方案都会对我有很大帮助!

[HttpPost]
public ActionResult FormPost(LogOnModel model)
{
WebRequest request = WebRequest.Create(strServer);
            // Set the Method property of the request to POST.
            request.Method = "POST";
            // Create POST data and convert it to a byte array.
            byte[] byteArray = Encoding.UTF8.GetBytes("password=" + model.Password);
            // Set the ContentType property of the WebRequest.
            request.ContentType = "application/x-www-form-urlencoded";
            // Set the ContentLength property of the WebRequest.
            request.ContentLength = byteArray.Length;
            // Get the request stream.
            Stream dataStream = request.GetRequestStream();
            // Write the data to the request stream.
            dataStream.Write(byteArray, 0, byteArray.Length);
            // Close the Stream object.
            dataStream.Close();
            // Get the response.
            WebResponse response = request.GetResponse();
            // Get the stream containing content returned by the server.
            dataStream = response.GetResponseStream();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader(dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd();
            // Clean up the streams.
            reader.Close();
            dataStream.Close();
            response.Close();
TempData["Response"] = responseFromServer;
return View();

您可以捕获身份验证操作发送的cookie,并将其附加到响应中,以便执行请求的实际用户在其浏览器中获得此cookie:

[HttpPost]
public ActionResult FormPost(LogOnModel model)
{
    using (var client = new WebClient())
    {
        var data = new NameValueCollection
        {
            { "username", "foo" },
            { "password", "bar" },
        };
        var result = client.UploadValues("http://localhost:1631/account/logon", data);
        var cookie = client.ResponseHeaders[HttpResponseHeader.SetCookie];
        Response.AddHeader("Set-Cookie", cookie);
    }
    return View();
}

但是,使用通用的身份验证服务似乎是一个更好的主意,而不是进行屏幕抓取。

假设使用相同的cookie域,您可以简单地使用ajax请求将凭据发送到其他站点。如果成功,您的控制器将返回一个ajax结果。查看MVC4模板,因为它们提供了ajax风格的登录。

最新更新