这个星期做了一个API接口整合的程序大家还是看程序把!
PDO统一接口开发规范1.0 加密方式
密码加密是MD5 32位加密
syskey 加密是32位安全码
功能描述:
1: 从A网站登录到B网站(A网站也同时登录)
2: 从B网站登录到A网站(B网站也同时登录)
注意:
1:俩个站点的syskey要一样。
2:俩个站点的用户名和密码要一样。
3:俩个站点是用XML传输的 编码是gb2312
使用说明:
1:在项目登录页中添加如下代码:
#region 动易,动网,Oblog 整合 编写人:JONE 编写时间:2011年1月13日 16:54:30
if (PDOAction.Instance().Enabled){
PDOAction.Instance().Login(Username.Text, hashPassword, 1);
}
#endregion
请求
格式:
POST HTTP/1.1 Content-Type: text/xml; charset=gb2312
<?xml version="1.0" encoding="gb2312"?>
<root>
<appid>程序标识</appid>
<action>操作类型</action>
<syskey>安全码</syskey>
<username>用户名</username>
<可选元素列表 />
</root>
提供 API 请求类 (PDOAction类)
/// <summary>
/// 是否开启整合
/// </summary>
/// <returns> true|false </returns>
public bool Enabled
{
get {
string enable = APIConfigs.GetConfigParam( " API_Enable " );
SysLog.WriterLog( " PDOAction.Enabled(是否开启整合) " + enable);
if (enable.ToLower() == " true " ){
return true ;
}
return false ;
}
}
/// <summary>
/// 整合登录
/// </summary>
/// <param name="username"> 用户名 </param>
/// <param name="password"> 用户密码(未加密) </param>
/// <param name="saveDays"> 保存Cookie的天数 </param>
public void Login( string username, string password, int saveDays)
{
try {
string LoginString = "" ;
Login(username, password, saveDays, ref LoginString);
Fetch.Trace(LoginString);
SysLog.WriterLog( " PDOAction.Login.LoginString= " + LoginString);
} catch (Exception ex){
SysLog.WriterLog( " PDOAction.Login.LoginString 错误:= " + ex.Message);
}
} public void Login( string username, string password, int saveDays, ref string LoginString)
{
PDOData pdo = new PDOData();
pdo.SetValue( " action " , " login " , 0 );
pdo.SetValue( " username " , username, 1 );
pdo.SetValue( " password " , password, 0 );
pdo.SetValue( " userstatus " , " 1 " , 0 );
pdo.SetValue( " status " , " 1 " , 0 );
string pwdkey = APIConfigs.GetConfigParam( " API_Key " );
pwdkey = Text.MD5(username + pwdkey);
pdo.SetValue( " syskey " , pwdkey, 0 );
int savecookie = 0 ;
if (saveDays >= 7 ) savecookie = 1 ;
if (saveDays >= 30 ) savecookie = 2 ;
if (saveDays >= 365 ) savecookie = 3 ;
pdo.SetValue( " savecookie " , savecookie.ToString(), 0 );
pdo.SendHttpData();
if (pdo.Status == " 1 " )
{
SysLog.WriterLog( " PDOAction.Login() " + pdo.Message);
// HttpContext.Current.Response.Write(pdo.Message);
// HttpContext.Current.Response.End();
}
HttpContext.Current.Response.Buffer = true ;
LoginString = pdo.SetCookie(username, password, savecookie.ToString());
}
响应
格式:
HTTP/1.1 200 OK Content-Type: text/xml; charset=gb2312 Content-Length: length
<?xml version="1.0" encoding="gb2312"?>
<root>
<appid>程序标识</appid>
<status>操作结果</status>
<body>
<message>操作不成功时返回的错误描述</message>
<可选元素列表 />
</body>
</root>
提供 API 响应的类( pdo 类)
private void Pdo_Load( object sender, EventArgs e)
{
#region 初始化操作
// 设置 HTTP 头
Response.ContentType = " text/xml " ;
Response.ContentEncoding = System.Text.Encoding.GetEncoding( " gb2312 " );
if (Get( " syskey " ) == "" || Get( " username " ) == "" ){
try
{
XmlTextReader reader = new XmlTextReader(Request.InputStream);
reader.WhitespaceHandling = WhitespaceHandling.None; xmlDoc.Load(reader);
} catch (XmlException xmle){
// this.__Finish(1, "获取xmlDoc失败=" + xmle.Message, true);
}
}
if ( ! PDOAction.Instance().Enabled){
this .__Finish( 1 , " 整合没有开启 " , true );
}
// 分析请求中的数据并将它们加载到 DNTParam[] 中
parameters = GetParamsFromRequest(Request);
// 验证安全码 syskey
if ( ! CheckSysKey()){
this .__Finish( 1 , " 安全密钥错误! " , true );
}
#endregion
#region 提交数据
if (CheckPost())
{
string action = Get( " action " );
switch (action.ToLower())
{
case " checkname " :{
CheckName();
break ;
}
case " reguser " :{
// RegUser();
break ;
}
case " login " :
{
UserLogin();
break ;
}
case " logout " :
{
Logout();
break ;
}
case " update " :
{
// UpdateUser();
break ;
}
case " delete " :
{
// DeleteUser();
break ;
}
case " lock " :
{
// LockUser();
break ;
}
case " getinfo " :
{
GetInfo();
break ;
}
case " checkemail " :
{
// CheckEmail();
break ;
}
default :{
SaveUserCookie();
this .__Finish( 1 , " 未指定操作类型 " , true );
break ;
}
}
}
#endregion
} private void __Finish( int status, string message, bool flg)
{
#region API提示信息
XmlTextWriter writer = null ;
try
{
writer = new XmlTextWriter(Response.Output);
writer.Formatting = Formatting.Indented;
writer.WriteStartDocument();
writer.WriteStartElement( " root " );
writer.WriteStartElement( " appid " );
writer.WriteString( " JXXYCMOA " );
writer.WriteEndElement();
writer.WriteStartElement( " status " );
writer.WriteString(status.ToString());
writer.WriteEndElement();
writer.WriteStartElement( " needcookie " );
if ( null != parameters)
{
writer.WriteString( " 1 " == Request.QueryString[ " needcookie " ] ? " 1 " : " 0 " );
}
writer.WriteEndElement();
writer.WriteStartElement( " body " );
writer.WriteStartElement( " message " );
writer.WriteString(message);
writer.WriteEndElement();
// 将可选的列表追加进来
if ( null != parameters && parameters.Length > 0 ){
foreach (DNTParam param in parameters){
if (param.Name != null && param.Name.Length > 0 ){
writer.WriteStartElement(param.Name);
writer.WriteString(param.Value);
writer.WriteEndElement();
}
}
}
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
}
finally
{
if ( null != writer)
{
writer.Close();
writer = null ;
}
}
#endregion
if (flg == true ){
Response.End();
}
}
private string Get( string key)
{
string tp = "" ;
if ( string .IsNullOrEmpty(Request.QueryString[key])){
if (xmlDoc != null )
tp = xmlDoc.DocumentElement.SelectSingleNode(key).InnerText.ToString();
} else {
tp = Request.QueryString[key].ToString();
}
if (tp.Length > 0 )
return tp;
else
return "" ;
}
private bool CheckPost()
{
string appid = Get( " appid " );
string syskey = Get( " syskey " );
string action = Get( " action " );
string username = Get( " username " );
string userTrueIP = Get( " userip " );
if (appid == null || syskey == null || username == null ){
this .__Finish( 1 , " 提交数据非法,操作中止! " , true );
return false ;
}
if (syskey != Text.MD5(username + APIConfigs.GetConfigParam( " API_Key " ))){
this .__Finish( 1 , " 所提交的api_key未关联到任何设定程序。 " + syskey, true );
return false ;
}
return true ;
}