1. 程式人生 > 實用技巧 >小白白的ASP.NET登入

小白白的ASP.NET登入

簡單的登陸 小白都會的東西,所以叫小白白的登陸,君仙是不是很有才呀 (☆_☆)/~~
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace MODEL
 8 {
 9     public class UserInfo
10     {
11         public int UserId { get; set; }
12         public
string LoginName { get; set; } 13 public string LoginPwd { get; set; } 14 } 15 }
Model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAL
{
    public class UserDAL
    {
        DBHelper db = new DBHelper();
        
//登入0 public int Login(string loginName, string loginPwd) { string sql = $"select count(*) from UserInfo where LoginName='{loginName}' and LoginPwd='{loginPwd}'"; return Convert.ToInt32(db.ExecuteScalar(sql)); } } }
Dal
 1 using System;
 2 using System.Collections.Generic;
3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using MODEL; 7 using DAL; 8 9 namespace BLL 10 { 11 public class UserInfoBLL 12 { 13 UserDAL dal = new UserDAL(); 14 //登入 15 public int Login(string loginName, string loginPwd) 16 { 17 return dal.Login(loginName, loginPwd); 18 } 19 } 20 }
Bll
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Net;
 5 using System.Net.Http;
 6 using System.Web.Http;
 7 using BLL;
 8 using MODEL;
 9 
10 namespace Exercise_API.Controllers
11 {
12     public class UserInfoController : ApiController
13     {
14         UserInfoBLL bll = new UserInfoBLL();
15         //登入
16         [HttpPost]
17         public IHttpActionResult Login(string loginName, string loginPwd)
18         {
19             return Json(bll.Login(loginName, loginPwd));
20 
21         }
22     }
23 }
Controllers
@{
    ViewBag.Title = "登入頁面";
}

<script src="~/Scripts/jquery-3.4.1.min.js"></script>
<script>
    //登入
    function Login() {
        var loginName = $("#textLogin").val();
        var loginPwd = $("#textPwd").val();

        if (loginName == "") {
            alert("使用者名稱不能為空");
            return;
        }

        $.ajax({
            url: "http://localhost:50262/api/UserInfo/Login?loginName=" + loginName + "&loginPwd=" + loginPwd,
            type: "post",
            dataType: "json",
            data: { Name: loginName },
            success: function (d) {
                if (d > 0) {
                    alert("登入成功");
                    location.href='/Product/Show'
                }
                else {
                    alert("登入失敗");
                }
            }
        })
    }

</script>

<table class="table">
    <tr>
        <td>使用者名稱:</td>
        <td><input id="textLogin" type="text" /></td>
    </tr>
    <tr>
        <td>密碼:</td>
        <td><input id="textPwd" type="password" /></td>
    </tr>
    <tr>
        <td colspan="2"><input type="button" value="登入" onclick="Login()" /></td>
    </tr>
</table>
Html