利用api接口来实现web网页登陆
因为公司的所有链接数据库的都是用的api接口 最近做了一个用api接口登陆
function Login() {
if (!Validate()) {
return false;
}
var para = {};
para.action = "login_by_api";
para.login_name = $.trim($("#txtLoginName").val());
para.password = $.trim($("#txtPassword").val());
$(".login-btn").attr("disabled", "disabled");
$.post("/handler/LoginHandler.ashx", para, function (data) {
if (data.ResultCode == 0) {
var result = data.ResultData[0];
loginInfo(result);
GetMessageCount();
NeedSetSearch();
location.href = location.href;
}
else {
$("#txtPassword").val("");
$("#txtLoginName").val("")
$(".login-error").html("用户名或密码错误");
ClearLogin(false);
}
$(".login-btn").removeAttr("disabled");
}, "json");
return true;
}
首先在控制器中调用一般处理程序
case "login_by_api":
{
#region 获取参数
string loginName = context.Request["login_name"];
string password = context.Request["password"];
#endregion
//执行查询
message = LoginByApi(loginName, password);
} break;
private string LoginByApi(string loginName, string password)
{
var result = new Result<object>();
try
{
var jss = new JavaScriptSerializer();
//API参数
ApiParam apiParam = new ApiParam();
apiParam.Add("login_name", loginName);
apiParam.Add("password", password);
apiParam.Add("source", 2);
var data = jss.Serialize(RequestAPIManager.Instance.Send(WebConstants.Login, apiParam.ToString()));
var rult = jss.Deserialize<ApiRult<UserInfo>>(data);
if (rult.Code == 0 && rult.Data != null && rult.Data.Code == "0")
{
List<object> ls = new List<object>();
SunTime03.Function.EcanSecurity es = new SunTime03.Function.EcanSecurity();
string ln = es.symmetry_Encode(loginName, "LoginCheck");
string pwd = es.symmetry_Encode(password, "LoginCheck");
var ui = rult.Data;
ls.Add(new { UserInfo = ui, LoginName = ln, Password = pwd });
result.ResultCode = ResultCodes.Success;
result.ResultData = ls;
SetCookie(ui.Account_Name, password, ui.Account_ID.ToString(), ui.Full_Name, "", "", "", "", "", ui.login_id, "", ui.Organization_ID, ui.Mobile, ui.Email, ui.Organization_Name, ui.NickName, ui.Token);
}
else
{
result.SetFail(ResultCodes.NotMatchedResource, "登陆失败!");
}
}
catch (Exception ex)
{
result.SetFail(ex.Message.ToString());
LogHelper.WriteLog(typeof(LoginHandler), ex);
}
return new JavaScriptSerializer().Serialize(result);
}
让后就是传入api参数,得到返回值
就这样的