我对 ajax 调用的登录函数有疑问,这是该函数的代码,它是一个可见的 Web 服务
[WebMethod]
[System.Web.Script.Services.ScriptMethod]
public string CheckData(string login, string Pass)
{
global gb = new global();
if (gb.CheckUserExist(login, Pass))
{
System.Web.Security.FormsAuthentication.RedirectFromLoginPage(login, true);
HttpCookie cookie = new HttpCookie("userData",login);
cookie.Expires = DateTime.Now.AddMonths(2);
Mosab2aModel.Mosab2aEntities context = new Mosab2aModel.Mosab2aEntities();
var User = context.Users.Where(x => x.UserName == login && x.Password == Pass)
.Select(x => new { x.UserName, x.Password, x.Admin, x.DisplayName, x.FBID }).First();
cookie["UserName"] = User.UserName;
cookie["Password"] = User.Password;
cookie["isAdmin"] = User.Admin.ToString();
cookie["Name"] = User.DisplayName;
cookie["FBID"] = User.FBID;
Context.Response.Cookies.Add(cookie);
//Context.Response.Redirect("/Default.aspx");
return "1";
}
else
{
return "0";
}
}
我在这里用 ajax 来称呼它
$.ajax({
type: "POST",
url: 'LoginService.asmx/CheckData',
data: "{'login':'"+ login +"','Pass':'"+pass +"'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg)
{
if (msg.d == "1")
{
//document.location.href = '/Default.aspx';
}
else
{
formWrapper.clearMessages();
displayError('Username or password is incorrect');
}
},
error: function()
{
formWrapper.clearMessages();
displayError('Error in contacting server , try again later.');
}
});
如果用户不存在,我有一个奇怪的问题,该函数运行良好,它返回值 0 并显示错误,但如果该函数找到用户,则会收到 401 错误未授权。
编辑:经过几次搜索,问题出在这一行!
System.Web.Security.FormsAuthentication.RedirectFromLoginPage(login, true);