日期:2014-05-17  浏览次数:20527 次

下午把OpenId尝试了下,可以用了,上一下混乱的代码
测试的提供商是myopenid
代码很乱。发上来做个记录。


C# code


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Net;
using System.Text;
using System.IO;

namespace MyOpenIdRP
{
    public partial class Login : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {

            WebRequest requ = WebRequest.Create("https://www.myopenid.com/server");
            requ.Method = "POST";
            requ.ContentType = "application/x-www-form-urlencoded";
            StringBuilder builder = new StringBuilder();
            //HtmlForm f = new HtmlForm();
            //f.ID = "finalForm";
            //f.Method = "POST";
            //f.Action = "https://www.myopenid.com/server";
            //f.EnableViewState = false;
           // f.Controls.Add(ProvideHiddenInput("openid.mode","check_authenticate"));
            

            Response.Write("==============================================Query String Start=======================<br/>");
            foreach(string str in Request.QueryString.AllKeys)
            {
                Response.Write(string.Format("[{0}]: {1} <br/>",str,Request.QueryString[str]));
                if (str == "openid.mode")
                {
                    //f.Controls.Add(ProvideHiddenInput(str, "check_authentication"));
                    builder.AppendFormat("{0}={1}", str, "check_authentication");
                    builder.Append('&');
                }
                else
                {
                    //f.Controls.Add(ProvideHiddenInput(str, Request.QueryString[str]));
                    builder.AppendFormat("{0}={1}", str, Request.QueryString[str]);
                    builder.Append('&');
                }
            }
            
            Response.Write("==============================================Query String Ended=======================<br/>");
            Response.Write("<br/><br/>");
            Response.Write("==============================================Params Start=======================<br/>");
            foreach (string str in Request.Params.AllKeys)
            {
                Response.Write(string.Format("[{0}]: {1} <br/>", str, Request.Params[str]));
            }

            byte[] formData = Encoding.UTF8.GetBytes(builder.ToString());
            requ.GetRequestStream().Write(formData,0,formData.Length);
            WebResponse resp = requ.GetResponse();
            StreamReader reader = new StreamReader(resp.GetResponseStream());
            Response.Write("====================================authenticate result========================<br/>");
            Response.Write(reader.ReadToEnd().Contains("is_valid:true"));
        }

        private HtmlInputHidden ProvideHiddenInput(string idname,string value) 
        {
            HtmlInputHidden hidden = new HtmlInputHidden();
            hidden.ID = idname;
            hidden.Attributes.Add("value", value);
            return hidden;
        }
    }
}





------解决方案--------------------
这个是干嘛呢
单点登陆吗?

如果是的话,有点不安全呢
------解决方案--------------------
探讨

这个是干嘛呢
单点登陆吗?

如果是的话,有点不安全呢