• [C#] Request.QueryString()测试:用html而非asp控件实现简单登录验证并保存值到Session中


    xxx.aspx页面和对应的xxx.aspx.cs的数据传递,然后实现登录验证并且将值保存到Session中,上次有用Ajax测试实现了,但是后面发现一个问题,Ajax传递到cs页面之后,如果要在被加上了 [WebMethod] 参数的静态方法中使用Session的话就会报错“  "未将对象引用设置到对象的实例。”

    [WebMethod]
    public static string GetUserName() 
    {
    String username=Session.["role"].toString();//这里会报错
    //......
     }

    如果想要在这个方法中使用Session,还要给这个方法加点东西。

    [WebMethod(EnableSession = true)]
    //或改为[WebMethod(true)]
    //将静态方法上面的[Method]的EnableSession值改为true就可以了

    到这里,结合上一篇发的博文,我遇到的问题算是解决了。

    这里还有另一种简单的方法,举个例子来说: A.aspx将用户输入的账号和姓名或者其他信,在点击提交按钮后传到 B.aspx页面中,然后 B.aspx.cs接收到A.aspx发送过来的数据。听起来比上一个方法麻烦,不过既然学习到了就写下来。代码参考如下

    A.aspx   (A.aspx.cs这里不需要改)

    <!--body部分-->
    <body>
    <form action="getpost.aspx" method="post">
              <input type="submit" ID="btnSearch" name="btnSearch" value="登录" class="button btn" />
              <input id="username" name="username" class="text" value="<%=Request.QueryString["key"] %>" type="text" />
              <input id="password" name="password" class="text" value="<%=Request.QueryString["key"] %>" type="text" />
     </form>
    </body>

    B.aspx  而这个页面只需要保留下面这一行就可以

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="B.aspx.cs" Inherits="TestASP_HTml.B" %>

    B.aspx.cs  

                //放到Page_Load方法内
                string username = Server.UrlEncode(Request.Form["username"]);
                String password = Server.UrlEncode(Request.Form["password"]);
    
    
                Database1Entities db = new Database1Entities();
                var result =
                    from ee in db.Table
                    where ee.Id.Equals(username )
                    where ee.name.Equals(password )
                    select ee;
    
                foreach (var kk in result)
                {
                    //登录成功!跳转页面
                    Response.Redirect("WebForm1.aspx");
                }        

    这样就ok了。

  • 相关阅读:
    base64编码
    ios开发之指纹识别
    date
    php的学习
    mac下安装mysql遇到的无法连接的问题
    关于git上传文件过大报错的问题 remote: warning: Large files detected.
    安卓开发中Theme.AppCompat.Light的解决方法
    ubuntu操作系统中卸载mysql的安装与卸载
    重新格式化删除U盘隐藏分区与如何在LMT下安装非Ghost win7
    网易有道笔试2015-05-12
  • 原文地址:https://www.cnblogs.com/Damocles-Sword/p/13199966.html
Copyright © 2020-2023  润新知