WEB开发网
开发学院WEB开发ASP asp写的openid登录方法 阅读

asp写的openid登录方法

 2009-04-23 10:40:53 来源:WEB开发网   
核心提示:以前好像看到sipo说asp写openid有些不能解决的问题,我也没查openid的规范,asp写的openid登录方法,这个对付简单的登录还是没问题的,不过代码还没测试

以前好像看到sipo说asp写openid有些不能解决的问题,我也没查openid的规范。
这个对付简单的登录还是没问题的,不过代码还没测试。

先放上来,有兴趣的可以看看


 <script language="JScript" runat="server">
/*
ASP Openid
by fisker
2008-9-3
*/
var alert=PRint=write=function(str){Response.Write(str);}
var openid = new fiskerOpenid();
var input=getInput();
if(input["openid.mode"]=="id_res"){
  var nickname = input["openid.sreg.nickname"];
  var email = input["openid.sreg.email"];
  var returnUrl=input["return"];
  returnUrl += returnUrl.indexOf("?")!=-1?"&":"?";
  returnUrl += "nickname="+nickname;
  returnUrl += "&email="+email;

  Response.Redirect(returnUrl);
}else{
  if(input["openid"]==""||input["return"]==""){
   Response.write("<sc"+"ript>");
   Response.write("alert(\"error\");");
   Response.write("window.history.forward(1);");
   Response.write("</sc"+"ript>");
   Response.end();
  }
 
  openid.url = input["openid"];
  openid.get();
}

function getInput(){
   var input=new Array();
   var e=new Enumerator(Request.QueryString);
   for (;!e.atEnd();e.moveNext()){
    x=e.item();
    input[String(x).toLowerCase()]=String(Request.QueryString(x));
   }
   e=new Enumerator(Request.Form);
   for (;!e.atEnd();e.moveNext()){
    x=e.item();
    input[String(x).toLowerCase()]=String(Request.Form(x));
   }
   return input;
}

function fiskerOpenid(){
  this.url="";
  this.server="";
  this.delegate="";
  this.user = null;
  this.trust_root="";
  this.return_to="";
  this.sreg_required="nickname,email";

  this.get = function(info){
   if(!this.init()){Response.write("error");return;}
   if(info!=undefined)this.sreg_required=info;
   var html=this.request(this.server,"openid.mode=associate&openid.assoc_type=HMAC-SHA1");
   if (html==""){Response.write("error");return;}
   var assoc_handle = "";
   var assoc_type = "";
   var expires_in = "";
   var mac_key = "";
   try{
    var re=new RegExp("assoc_handle:(.+)","ig");
    assoc_handle = re.exec(html)[1];
    var re=new RegExp("assoc_type:(.+)","ig");
    aassoc_type = re.exec(html)[1];
    var re=new RegExp("expires_in:(.+)","ig");
    expires_in = re.exec(html)[1];
    var re=new RegExp("mac_key:(.+)","ig");
    mac_key = re.exec(html)[1];
    delete re;
   }catch(e){}

   var url = this.server;
   url += url.indexOf("?")!=-1?"&":"?";
   url += "openid.mode="+Server.URLEncode("checkid_setup");
   url += "&openid.identity="+Server.URLEncode(this.delegate);
   url += "&openid.trust_root="+Server.URLEncode(this.trust_root);
   url += "&openid.return_to="+Server.URLEncode(this.return_to);
   url += "&openid.assoc_handle="+Server.URLEncode(assoc_handle);
   url += "&openid.sreg.required="+Server.URLEncode(this.sreg_required);

   Response.Redirect(url);
  }

  this.init = function(){
   if(!this.check()) return false;

   if(this.trust_root==""||this.return_to==""){
    var thisServer="";
    var thisUrl="";
    thisServer+=String(Request.serverVariables("https")).toLowerCase()=="off"?"http://":"https://";
    thisServer+=String(Request.serverVariables("server_name"));
    thisServer+=String(Request.serverVariables("server_port"))=="80"?"":":"+String(Request.ServerVariables("server_port"));
    thisUrl+=thisServer;
    thisUrl+=String(Request.serverVariables("URL"));
    thisUrl+=String(Request.QueryString)==""?"":"?"+String(Request.QueryString);
    this.trust_root=(this.trust_root==""?thisServer:this.trust_root);
    this.return_to=(this.return_to==""?thisUrl:this.return_to);
   }

   var html = this.request(this.url);
   if (html=="") return false;
   html=html.replace(/\'/ig,"\"");
   var re=new RegExp("<link(.*?)rel=\"(.*?)\"(.*?)href=\"(.*?)\"(.*?)>","ig");
   var arr=[];
   while ((arrMatch = re.exec(html)) != null){
    var index=arrMatch[2].toLowerCase();
    var value=arrMatch[4];
    if(index.indexOf(" ")){
     for(var j=0;j<index.split(" ").length;j++)arr[index.split(" ")[j]]=value;
    }else{
     arr[index]=value;
    }
   }
   delete re;
   if(arr["openid.server"]!=""){
    this.server=arr["openid.server"];
   }else{
    return false;
   }
   this.delegate = arr["openid.delegate"]==undefined?this.url:arr["openid.delegate"];
   return true;
  }

  this.check = function(){
   if (this.url=="") return false;
   if(this.url.indexOf("://")==-1) this.url="http://"+this.url;
   return true;
  }

  this.request = function(url,data){
   if (data == undefined)var data = null;
   var method = data ? 'POST' : 'GET';
   var html="";
   try{
    var xmlhttp=Server.CreateObject("MSXML2.Serverxmlhttp.3.0");
    xmlhttp.open(method,url,false);
    xmlhttp.send(data);
     html = this.bytesToStr(xmlhttp.responseBody);
    delete xmlhttp;
   }catch(e){}
    return html;
   }

  this.bytesToStr = function(CodeBody){
   var objStream= Server.CreateObject("ado"+"db.stream") ;
   objStream.Type = 1 ;
   objStream.Mode =3 ;
   objStream.Open() ;
   objStream.Write(CodeBody);
   objStream.Position = 0 ;
   objStream.Type = 2 ;
   objStream.charset = "utf-8" ;
   var returnstr = objStream.ReadText;
   objStream.Close();
   delete objStream;
   return returnstr;
  }

}
</script>

作者:fisker

Tags:asp openid 登录

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接