AI智能
改变未来

C# WebApi+MVC 实现登录验证功能

DAL传入参数 与数据库账号密码匹配public List<UserInfo> Login(string username,string userpwd){string sql = $\"select * from UserInfo where UserName=\'{username}\' and UserPwd=\'{userpwd}\'\";return DBHelper.GetToList<UserInfo>(sql);}
Api控制器判断如果有数据返回查询数据 没有则返回空[HttpGet][Route(\"api/Login\")]public IHttpActionResult Login(string username, string userpwd){List<UserInfo> list = bll.Login(username,userpwd);if (list.Count>0){var models = (from s in list select s).FirstOrDefault();return Ok(models);}else{return Ok(\"\");}}
MVC视图界面$(\"#BtnLogin\").click(function () {var UserName = $(\"#UserName\").val(); //获取用户名var UserPwd = $(\"#UserPwd\").val();//获取密码if (UserName == \"\" || UserName == null) {//判断账号是否为空 给出提示alert(\"账号不能为空\");return;}else if (UserPwd == \"\" || UserPwd == null) {alert(\"密码不能为空\");return;}//调用ajax 实现登录功能$.ajax({url: \"http://localhost:52432/api/Login\",data: { UserName: UserName, UserPwd: UserPwd  },//传入参数type: \"get\",dataType: \"json\",success: function (result) {//根据结果给出提示if (result==\"\") {alert(\"用户名或密码不正确\");return;}//如果不为空 提示登录成功跳转到显示界面else {alert(\"登录成功\");location.href = \"ShowList\";}}});});
赞(0) 打赏
未经允许不得转载:爱站程序员基地 » C# WebApi+MVC 实现登录验证功能