asp.net对json文件或者txt文件进行调用

来源:互联网 发布:java api文档怎么看 编辑:程序博客网 时间:2024/06/02 07:28

1.简单登录验证 :

try

            {
                var id = txtUName.Text;
                var password = txtPW.Text;
                if (string.IsNullOrEmpty(id))
                {
                    spUserName.InnerHtml = "<label style=\"display: block;\" class=\"error\" generated=\"true\" for=\"txtUName\">用户名不能为空!</label>";
                    return;
                }
                if (string.IsNullOrEmpty(password))
                {
                    spPW.InnerHtml = "<label style=\"display: block;\" class=\"error\" generated=\"true\" for=\"txtPW\">密码不能为空!</label>";
                    return;
                }


                string path = HttpRuntime.AppDomainAppPath.ToString() + "\\Extension\\UserInfo.json";
                //读取json文件  
                using (StreamReader sr = new StreamReader(path))
                {
                    List<HTUser> users = new List<HTUser>();
                    JsonSerializer serializer = new JsonSerializer();
                    serializer.Converters.Add(new JavaScriptDateTimeConverter());
                    serializer.NullValueHandling = NullValueHandling.Ignore;


                    //构建Json.net的读取流  
                    JsonReader reader = new JsonTextReader(sr);
                    //对读取出的Json.net的reader流进行反序列化,并装载到模型中  
                    users = serializer.Deserialize<List<HTUser>>(reader);
                    if (users != null && users.Count > 0)
                    {
                        for (int i = 0; i < users.Count; i++)
                        {
                            if (users[i].id == id && users[i].password == password)
                            {
                                Application["id"] = id;
                                Response.Redirect("HTIndex.aspx");
                            }
                        }
                        Response.Write("<script>alert('用户名或者密码错误!');</script>");
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Error("Login()方法错误:" + ex.ToString());
            }
原创粉丝点击