cookis

/// <summary>
/// 登录
/// </summary>
/// <param name="ACount"></param>
/// <param name="Pwd"></param>
/// <returns></returns>
[HttpGet]
public UsersInfo Login(string ACount = "", string Pwd = "")
{
var list = myContext.UsersInfos.Where(s => s.ACount == ACount && s.Pwd == Pwd).ToList();
if (list != null || list.Count > 0)
{
return list.FirstOrDefault();
}
else
{
return null;
}
}

-------------------------

@{
Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>LoginIndex</title>
<link href="~/Content/bootstrap.css" rel="stylesheet" />
<script src="~/Scripts/jquery-3.4.1.js"></script>
<script>
//文档加载函数
$(function () {
if (getCookie("UsersInfo") == null) {
setCookie("UsersInfo", "[]");
}
else {
var list = JSON.parse(getCookie("UsersInfo"));
var obj = {
ACount: list[0].ACount,
Pwd: list[0].Pwd
}
Aj(obj);
}
})
function Login() {
var obj = {
ACount: $("#ACount").val(),
Pwd: $("#Pwd").val(),
}
Aj(obj);

}
function Aj(obj) {
$.ajax({
url: "http://localhost:57190/api/Sale/Login",
data: obj,
type: "get",
dataType: "json",
success: function (d) {
if (d != null) {
var list = JSON.parse(getCookie("UsersInfo"));
var st = {
ACount: $("#ACount").val(),
Pwd: $("#Pwd").val(), UId: d.UId
};
list.push(st);
var data = JSON.stringify(list);
setCookie("UsersInfo", data);
alert('登录成功');
location.href = '/Sale/OrdersInfoIndex?UId=' + d.UId;
}
else {
alert('账号或密码输入错误');
}
}
})
}


/* cookie中存值
*/
function setCookie(name, value) {
if (value) {
var days = 1; //定义一天
var exp = new Date();
exp.setTime(exp.getTime() + days * 24 * 60 * 60 * 1000);
// 写入Cookie, toGMTString将时间转换成字符串
document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString;
}
};

/**
* cookie中取值
* */
function getCookie(name) {
var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)"); //匹配字段
if (arr = document.cookie.match(reg)) {
return unescape(arr[2]);
} else {
return null;
}
};
</script>
</head>
<body>
@using()
<table class="table" style="margin:0 auto;85%;height:85%;text-align:center">
<tr>
<td>账号:</td>
<td><input id="ACount" type="text" placeholder="请输入您的账号" /></td>
</tr>
<tr>
<td>密码:</td>
<td><input id="Pwd" type="password" placeholder="请输入您的密码" /></td>
</tr>
<tr>
<td colspan="2"><input id="Button1" type="button" value="登录" onclick="Login()" /></td>
</tr>
</table>
</body>
</html>

原文地址:https://www.cnblogs.com/gc1229/p/13276198.html