以前好像看到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>