您的位置:首页 > 编程语言 > ASP

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

2017-11-06 15:05 211 查看
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());

            }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐