CheckLogin.cs

本文介绍了一个用户登录验证及管理员权限检查的实现方法,通过SQL查询来确认用户名和密码的有效性,并进一步判断用户是否为管理员身份。此外,还提供了获取特定用户类型的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

using System;
2using System.Data;
3using System.Data.SqlClient;
4using System.Configuration;
5using System.Web;
6using System.Web.Security;
7using System.Web.UI;
8using System.Web.UI.WebControls;
9using System.Web.UI.WebControls.WebParts;
10using System.Web.UI.HtmlControls;
11
12/// <summary>
13/// CheckLogin 的摘要说明
14/// </summary>

15public class CheckLogin
16{
17    public CheckLogin()
18    {
19        //
20        // TODO: 在此处添加构造函数逻辑
21        //
22    }

23    public int GiveID(string UserName, string PassWord)
24    {
25        SqlDataBase ThisB = new SqlDataBase();
26        string CheckSql = "select count(*) from UserList where UserName = '" + UserName + "' and PassWord = '" + PassWord + "'";
27        int ThisNum = Convert.ToInt32(ThisB.ReturnSQL(CheckSql));
28        if (ThisNum > 0)
29        {
30            string SqlType = "select * from UserList where UserName = '" + UserName + "' and PassWord = '" + PassWord + "'";
31            SqlDataReader ThRead = ThisB.GRead(SqlType);
32            while (ThRead.Read())
33            {
34                return Convert.ToInt32(ThRead["ID"]);
35            }

36        }

37        return 0;
38    }

39    public static bool CheckAdmin(int UserID)
40    {
41        SqlDataBase MyDb = new SqlDataBase();
42        string Log = Convert.ToString(MyDb.ReturnSQL("select UserType from UserList Where ID = '" + UserID + "'"));
43        if (Log == "1")
44        {
45            return true;
46        }

47        else
48        {
49            return false;
50        }

51    }

52    public static int GiveCheckAd(int UserID)
53    {
54        SqlDataBase MyDb = new SqlDataBase();
55        int Log = Convert.ToInt32(MyDb.ReturnSQL("select GoodsType from UserList Where ID = '" + UserID + "'"));
56        return Log;
57    }

58    public static DataTable GiveMsgGod(int UserID) //返回对应仓库的数量超限产品
59    {
60        SqlDataBase MyDb = new SqlDataBase();
61        int Log = Convert.ToInt32(MyDb.ReturnSQL("select Controls from UserList Where ID = '" + UserID + "'"));
62        string SqlString = "";
63        if (Log == 0)
64        {
65            SqlString = "select * from View_GoodMsg";
66        }

67        else
68        {
69            SqlString = "select * from View_GoodMsg where ID = '"+Log+"'";
70        }

71        return MyDb.DT(SqlString);
72    }

73    public static bool CheckKeepAdmin(int UserID)
74    {
75        SqlDataBase MyDb = new SqlDataBase();
76        string Log = Convert.ToString(MyDb.ReturnSQL("select GoodsType from UserList Where ID = '" + UserID + "'"));
77        if (Log == "0")
78        {
79            return true;
80        }

81        else
82        {
83            return false;
84        }

85    }

86}


package com.cissst.servlet; import java.io.IOException; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.cissst.entity.Admin; import com.cissst.entity.CustomAccount; import com.cissst.service.IAdminService; import com.cissst.service.ICustomAccountService; import com.cissst.service.impl.AdminServiceImpl; import com.cissst.service.impl.CustomAccountServiceImpl; import com.cissst.util.MD5Util; import com.mysql.cj.AbstractQuery; public class UserServlet extends HttpServlet { @Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); String action = request.getParameter("action"); IAdminService as = new AdminServiceImpl(); ICustomAccountService cs = new CustomAccountServiceImpl(); HttpSession session = request.getSession(); switch (action) { case "login": handleLogin(request, response, session, as, cs); break; case "logout": case "relogin": handleLogout(session, response); break; } } private void handleLogin(HttpServletRequest request, HttpServletResponse response, HttpSession session, IAdminService adminService, ICustomAccountService accountService) throws IOException { String username = request.getParameter("username"); String password = MD5Util.encode(request.getParameter("password")); String userType = request.getParameter("usertype"); // 根据用户类型验证登录 if ("admin".equals(userType)) { Admin admin = adminService.findBynp(username, password); if (admin != null) { session.setAttribute("admin", admin); response.sendRedirect("index.jsp"); return; } } else if ("user".equals(userType)) { CustomAccount account = accountService.findBynp(username, password); if (account != null) { session.setAttribute("custom_Account", account); response.sendRedirect("index2.jsp"); return; } } // 统一处理登录失败 handleLoginFailure(request, response); } private void handleLogout(HttpSession session, HttpServletResponse response) throws IOException { if (session != null) { session.invalidate(); } response.sendRedirect("login.jsp"); } private void handleLoginFailure(HttpServletRequest request, HttpServletResponse response) throws IOException { // 使用传入的 request 对象 HttpSession session = request.getSession(); session.setAttribute("loginError", "用户名或密码错误"); response.sendRedirect("login.jsp"); } } at com.cissst.servlet.UserServlet.service(handleLogin(request, response, session, as, cs);)
最新发布
08-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值