html 页面调用ASP.NET 进行用户名密码验证

  在B/S应用程序中,前台与后台的数据交互,都是通过HTML中form表单完成的。form提供了两种数据传输的方式:get和post,用个“登录”这个例子来简单理解二者提交和获取方式的不同。

提交

在HTML中写post方式提交

 
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

  2. "http://www.w3.org/TR/html4/loose.dtd">

  3. <html>

  4. <head>

  5. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">

  6. <title>登录</title>

  7. <style type="text/css">

  8. </style>

  9. </head>

  10. <body>

  11. <!--from表单中method的post和get方式-->

  12. <form name="form1" method="post" action="login.aspx">

  13. <table width="320" height="153" border="0" align="center" class="BorderSyle">

  14. <tr>

  15. <td width="89" align="right">用户名:</td>

  16. <td width="215">

  17. <input name="userName" type="text" class="BorderSyle" id="textUserName"></td>

  18. </tr>

  19. <tr>

  20. <td align="right">密 码:</td>

  21. <td>

  22. <input name="userPwd" type="text" id="textUserPwd"></td>

  23. </tr>

  24. <tr>

  25. <td colspan="2" class="auto-style1">

  26. <input type="submit" name="Submit" value="提交">

  27. <input type="submit" name="Submit2" value="重置"></td>

  28. </tr>

  29. </table>

  30. </form>

  31. </body>

  32. </html>

 
  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. using System.Data.SqlClient;

  8.  
  9.  
  10. namespace LoginTest

  11. {

  12. public partial class Login : System.Web.UI.Page

  13. {

  14. protected void Page_Load(object sender, EventArgs e)

  15. {

  16.  
  17. //定义两个变量,获取从表单上提交的用户名和密码

  18. string userName = Request.Form["userName"].ToString();

  19. string userPwd = Request.Form.Get("userPwd").ToString();

  20.  
  21. //连接数据库

  22. SqlConnection con = new SqlConnection("Server=.;database=login;uid=sa;pwd=123456");

  23. con.Open();

  24.  
  25. //用一个命令对象去查找数据库中这个用户是否存在

  26. SqlCommand cmd = new SqlCommand("select count(*) from login where userName='" + userName + " 'and userPwd='" + userPwd + "' ", con);

  27.  
  28.  
  29. //判断用户是否存在

  30. int count = Convert.ToInt32(cmd.ExecuteScalar());

  31. if (count > 0)

  32. {

  33. //用户存在

  34. Response.Redirect("main.aspx?userName=" + userName); //get提交

  35. //Response.Redirect("main.aspx"); //post提交

  36. }

  37. else

  38. {

  39. //用户不存在

  40. Response.Redirect("loginFail.html");

  41. }

  42. }

  43. }

  44. }

获取

 
  1. using System;

  2.  
  3. using System.Collections.Generic;

  4.  
  5. using System.Linq;

  6.  
  7. using System.Web;

  8.  
  9. using System.Web.UI;

  10.  
  11. using System.Web.UI.WebControls;

  12.  
  13.  
  14. namespace LoginTest

  15.  
  16. {

  17.  
  18. public partial class main : System.Web.UI.Page

  19.  
  20. {

  21.  
  22. protected void Page_Load(object sender, EventArgs e)

  23.  
  24. {

  25.  
  26. //get提交,获取用户名的方法

  27.  
  28. string userName = Request.QueryString["userName"].ToString();

  29.  
  30. //由服务器端向客户端书写

  31.  
  32. Response.Write("<font size=24 color=red align=center>欢迎" + userName + "光临天猫购物</font>"); //get提交所用的获取方法

  33.  
  34.  
  35. // Response.Write("<font size=24 color=red >欢迎光临天猫购物</font>"); //post提交所用的获取方法

  36.  
  37. }

  38.  
  39. }

  40.  
  41. }

get提交的时候,它的变量和值都会在地址栏里显示出来,它的安全性有一定的影响。从一个页面到另一个页面传参的时候,可以使用get方法,速度更快些。

               

总结

1、get是用来从服务器上获得数据,而post是用来向服务器上传递数据。

2、get在传输过程,表单中数据的按照variable=value的形式,添加到action所指向的URL后面,并且两者使用“?”连接,数据被放在请求的URL中,服务器会将请求URL记录到日志文件中,然后放在某个地方,这样就可能会有一些隐私的信息被第三方看到。另外,用户也可以在浏览器上直接看到提交的数据,一些系统内部消息将会一同显示在用户面前。而post是将表单中的数据放在form的数据体中,其所有操作对用户来说都是不可见的。

                                       

      总之,个人对于get和post的理解,最大的不同就是一个用于获取数据,一个用于修改数据。

--------------------- 本文来自 JanneyTan 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/u010927640/article/details/50085121?utm_source=copy

html部分

 
  1. <form method="post" action="http://www.xxx.com/Login.aspx">

  2. <label>用户名  </label> <input type="text" name="_user" id="username" size="8" />

  3. <label>密  码  </label> <input type="password" name="_pass" id="password" size="8" />

  4. <input type="image" name="button" src="images/index3.gif">

  5. </form>



 asp.net 代码如下:

 
  1. private MODEL_stu_info get_model()

  2. {

  3. string str_username = Request.Form["_user"];

  4. string str_password = Request.Form["_pass"];

  5. MODEL_stu_info model_stu = new MODEL_stu_info();

  6. model_stu.User_name = str_username;

  7. model_stu.Stu_password = str_password;

  8. return model_stu;

  9. }




 
通过下面的代码实现对用户名进行调用  

 

 string str_username = Request.Form["_user"];

--------------------- 本文来自 路口下车 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/a892886597/article/details/12019303?utm_source=copy

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>晓莫作品-平台登录页面</title>
    <link href="css/login.css" rel="stylesheet" />
    <script src="js/login.js"></script>
    <link rel="icon" href="../imgs/m.ico"/>
</head>
<body>
    <form id="form1" runat="server">
    <!--顶部窄导航条-->
    <div class="topnav">
        <div class="userInfo">
            <span>欢迎您,请登录你的账号!</span>
        </div>

        <ul class="linkwrap">
            <li><a href="http://www.moscte.com" target="_blank">平台首页</a></li>
            <li><a href="http://www.moscte.com" target="_blank">帮助中心</a></li>
        </ul>
    </div>
    <!--顶部宽导航条及LOGO-->
    <div class="mainnav">
        <a target="_blank" href="http://www.moscte.com">
            <div class="logo">
                <div class="imglogo">
                     <img alt="moscte logo" src="images/1.png" />
                </div>
                <div class="txtlogo">
                    <b>BETA!</b>
                    <h1>用户后台</h1>
                    <span>晓莫业务平台</span>
                </div>
            </div>
         </a>
            <div class="linknav" id="linknav">
                <ul>
                    <li class="scractive"><a target="_blank" href="http://www.moscte.com"><b>功能连接</b></a></li>
                    <li><a target="_blank" href="http://www.moscte.com"><b>功能连接</b></a></li>
                    <li><a target="_blank" href="http://www.moscte.com"><b>功能连接</b></a></li>
                </ul>
            </div>
         <div class="scr" id="scr"></div>
    </div>
    <!--图片展示及登录-->
        <!--图片展示-->
    <div class="showil" id="showil">
        <ul>
            <li><img alt="" src="../imgs/ad3.jpg" /></li>
            <li><img alt="" src="../imgs/ad2.jpg" /></li>
            <li><img alt="" src="../imgs/ad1.jpg" /></li>
            <li><img alt="" src="../imgs/ad4.jpg" /></li>
        </ul>
        <ol>
            <li class="active"></li>
            <li></li>
            <li></li>
            <li></li>
        </ol>
    </div>
        <!--登录窗口-->
    <div class="malogin">
        <h1>登录Moscte.com</h1>
        <input type="text" class="txt" value="MOID/电子邮箱/手机号码" />
        <input type="text" class="txt" value="登录密码"/>
        <input type="text" class="txtyz" value="验证码"/><b>ACCC</b><br />
        <img id="qr" alt="" src="http://ww1.sinaimg.cn/mw690/005Li8nIgw1evm3wiqyp2j303w014742.jpg" /><br /><br />
        <a target="_blank" href="http://www.moscte.com">找回密码</a><a target="_blank" href="http://www.moscte.com">使用协议</a>
    </div>
    <!--底部功能展示-->
    <div class="gnshow">
            <div class="imgshow">
                <a target="_blank" href="http://weibo.com/moscte/"><img alt="" src="http://ww3.sinaimg.cn/mw690/005Li8nIgw1evm3wj6r7rj308v044t97.jpg" /></a>
                <a target="_blank" href="http://www.moscte.com/xiaomo.aspx"><img alt="" src="http://ww1.sinaimg.cn/mw690/005Li8nIgw1evm3wjsod4j308u045mxi.jpg" /></a>
                <a target="_blank" href="http://www.moscte.com"><img alt="" src="http://ww4.sinaimg.cn/mw690/005Li8nIgw1evm3wkc7o1j308v045t8z.jpg" /></a>
             </div>
        </div>
    <hr />
     <!--底部信息展示--> 
    <div class="xxshow">
        <a target="_blank" href="http://www.moscte.com">关于晓莫</a>|
        <a target="_blank" href="http://www.moscte.com">功能特色</a>|
        <a target="_blank" href="http://www.moscte.com">用户协议</a>|
        <a target="_blank" href="http://www.moscte.com">运营资质</a><br />
        <b>Copyright 2015 ©</b><a target="_blank" href="http://www.moscte.com">莫欧科技</a>
        <b> All Rights Reserved</b><a target="_blank" href="http://www.moscte.com"> ICP备:1502346</a>
    </div>
    </form>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/update7/article/details/82908222