找回密码
 会员注册
查看: 178|回复: 0

asp.net登录界面制作实例

[复制链接]

250

主题

1

回帖

819

积分

管理员

积分
819
发表于 2024-2-29 08:21:04 | 显示全部楼层 |阅读模式

类库代码如下:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Data.SqlClient;
  6. using System.Configuration;
  7. using System.Data;
  8. /// <summary>
  9. ///CommonClass 的摘要说明
  10. /// </summary>
  11. public class CommonClass
  12. {
  13. public CommonClass()
  14. {
  15. }
  16. /// <summary>
  17. /// 数据库连接类
  18. /// </summary>
  19. /// <returns>连接对象</returns>
  20. public SqlConnection GetConnection()
  21. {
  22. string myStr = ConfigurationManager.AppSettings["ConnectionString"].ToString();
  23. SqlConnection myConn = new SqlConnection(myStr);
  24. return myConn;
  25. }
  26. /// <summary>
  27. /// 弹出框
  28. /// </summary>
  29. /// <param name="TxtMessage">弹出提示信息</param>
  30. /// <param name="Url">对话框关闭后,转到地址</param>
  31. /// <returns></returns>
  32. ///
  33. public string MessageBox(string TxtMessage,string Url) {
  34. string str;
  35. str = "<script language=javascript>alert('"+TxtMessage+"');location='"+Url+"'</script>";
  36. return str;
  37. }
  38. /// <summary>
  39. /// 用来执行增删改功能
  40. /// </summary>
  41. /// <param name="sqlStr"> 操作的SQL语句</param>
  42. /// <returns>成功返回true,失败返回FALSE</returns>
  43. ///
  44. public Boolean ExecSQL(string sqlStr) {
  45. SqlConnection myConn = GetConnection();
  46. myConn.Open();
  47. SqlCommand myCmd = new SqlCommand(sqlStr,myConn);
  48. try
  49. {
  50. myCmd.ExecuteNonQuery();
  51. myConn.Close();
  52. }
  53. catch {
  54. myConn.Close();
  55. return false;
  56. }
  57. return true;
  58. }
  59. /// <summary>
  60. /// 返回数据源的数据集
  61. /// </summary>
  62. /// <param name="sqlStr">操作SQL语句</param>
  63. /// <param name="TableName">数据表名称</param>
  64. /// <returns>数据集DataSet</returns>
  65. public DataSet GetDataSet(string sqlStr, string TableName) {
  66. SqlConnection myConn = GetConnection();
  67. myConn.Open();
  68. SqlDataAdapter adapt = new SqlDataAdapter(sqlStr, myConn);
  69. DataSet ds = new DataSet();
  70. adapt.Fill(ds, TableName);
  71. myConn.Close();
  72. return ds;
  73. }
  74. /// <summary>
  75. /// 验证登录,防止SQL注入式攻击
  76. /// </summary>
  77. /// <param name="loginName">用户名</param>
  78. /// <param name="loginPwd">密码</param>
  79. /// <returns></returns>
  80. public int checkLogin(string loginName,string loginPwd) {
  81. SqlConnection myConn = GetConnection();
  82. SqlCommand myCmd = new SqlCommand( "select count(*) from tb_User where Name=@loginName and PassWord=@loginPwd",myConn);
  83. myCmd.Parameters.Add(new SqlParameter("@loginName",SqlDbType.VarChar,20));
  84. myCmd.Parameters["@loginName"].Value = loginName;
  85. myCmd.Parameters.Add(new SqlParameter("@loginPwd", SqlDbType.VarChar, 50));
  86. myCmd.Parameters["@loginPwd"].Value = loginPwd;
  87. myConn.Open();
  88. int i = (int)myCmd.ExecuteScalar();
  89. myCmd.Dispose();
  90. myConn.Close();
  91. return i;
  92. }
  93. /// <summary>
  94. /// 实现随机验证码
  95. /// </summary>
  96. /// <param name="n">验证码个数</param>
  97. /// <returns>返回生成的随机数</returns>
  98. public string RandomNum(int n) {
  99. string strchar = "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z";
  100. string[] VcArray = strchar.Split(',');
  101. string VNum = "";
  102. int temp = -1;
  103. Random rand=new Random();
  104. for (int i=1; i < n + 1; i++) {
  105. if (temp != -1) {
  106. rand = new Random(i*temp*unchecked((int)DateTime.Now.Ticks));
  107. }
  108. int t = rand.Next(61);
  109. if (temp != -1 && temp == t) {
  110. return RandomNum(n);
  111. }
  112. temp = t;
  113. VNum += VcArray[t];
  114. }
  115. return VNum;
  116. }
  117. }
复制代码

.aspx代码如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Login.aspx.cs" Inherits="Login" %>







   
   


   


   

       
           
            管理员姓名:
           
           

           
             管理员密码:
           
           

           
             验证码:
           
           
                abel ID="lab_Code" runat="server" Text="8888">abel>

           

            
               
        
           
       

                      οnclick="btn_login_Click1" />
             
                              οnclick="btn_cancel_Click1" />
   

   



.aspx.cs代码如下:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. public partial class Login : System.Web.UI.Page
  8. {
  9. CommonClass cc = new CommonClass();
  10. protected void Page_Load(object sender, EventArgs e)
  11. {
  12. if (!IsPostBack) {
  13. this.lab_Code.Text = cc.RandomNum(4);
  14. }
  15. }
  16. protected void btn_login_Click1(object sender, EventArgs e)
  17. {
  18. if (txt_name.Text.Trim() == "" || txt_pwd.Text.Trim() == "")
  19. {
  20. Response.Write(cc.MessageBox("登录名和密码不能为空!", "Login.aspx"));
  21. }
  22. else
  23. {
  24. if (txt_code.Text.Trim() == lab_Code.Text.Trim())
  25. {
  26. int IntUserIn = cc.checkLogin(txt_name.Text.Trim(), txt_pwd.Text.Trim());
  27. if (IntUserIn > 0)
  28. {
  29. Response.Write("<script language=javascript> window.open('AdminIndex.aspx');window.close();</script>");
  30. }
  31. else
  32. {
  33. Response.Write(cc.MessageBox("登录名或密码错误!", "Login.aspx"));
  34. }
  35. }
  36. else
  37. {
  38. Response.Write(cc.MessageBox("验证码错误!", "Login.aspx"));
  39. }
  40. }
  41. }
  42. protected void btn_cancel_Click1(object sender, EventArgs e)
  43. {
  44. Response.Write("<script>window.close();location='javascript:history.go(-1)';</script>");
  45. }
  46. }
复制代码
测试效果如下:




来源:https://blog.csdn.net/yayun0516/article/details/41913059
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?会员注册

×
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 会员注册

本版积分规则

QQ|手机版|心飞设计-版权所有:微度网络信息技术服务中心 ( 鲁ICP备17032091号-12 )|网站地图

GMT+8, 2024-12-27 00:42 , Processed in 0.362405 second(s), 26 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表