asp.net - 登录不正确时显示消息

标签 asp.net login error-handling

我已经尝试过此代码

当我使用不正确的用户名和密码登录时,它重定向到webform2.aspx,我要在其中显示消息错误的用户名和密码

我该怎么做?

我正在尝试下面的代码,但它不起作用

 protected void Button1_Click(object sender, EventArgs e)
    {
        if(loginmethod(txt_us.Text,txt_pwd.Text)!="NA")
        {
            FormsAuthentication.Initialize();
            String strRole =Assignroles(txt_us.Text);
            FormsAuthenticationTicket fat = new FormsAuthenticationTicket(1, txt_us.Text, DateTime.Now, DateTime.Now.AddMinutes(30), false, strRole, FormsAuthentication.FormsCookiePath);
            Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName,
            FormsAuthentication.Encrypt(fat)));
            loginmethod(txt_us.Text, txt_pwd.Text);
            Response.Redirect("WebForm2.aspx");
        }
        else
        {
            Label1.Text = ("Incorrect UserName/Password");
            Label1.Visible = true;
        }
        txt_us.Text = "";
        txt_pwd.Text = "";
    }
    private string loginmethod(string UserName, string Password)
    {
       try
       {
           login_class lg_class = new login_class();
           Entities2 login = new Entities2();
           string logn = Convert.ToString(lg_class.loginfu(UserName, Password).Rows[0]["id"]);
           Session["ID"] = logn.ToString();
           return (logn);
       }
       catch (Exception ex)
       {

           return (ex.Message.ToString());

       }

    }

登录方法
  public DataTable loginfunction(string username,string password)
        {
            try
            {
                Entities2 lg = new Entities2();
               List<SP_GetLogin_Result> gr = lg.SP_GetLogin(username, password).ToList();
                DataTable dt = new DataTable();
                dt.Columns.Add("id", typeof(string));
                foreach (var l in gr)
                {
                    dt.Rows.Add(l.id);
                }
                return dt;   
            }

           catch (Exception)
            {
                throw new Exception();

            }

        }

更新

我这样做但是这个显示错误
 Entities2 lg = new Entities2();
        DataTable dt = new DataTable();
        dt=lg.SP_GetLogin(username,password).ToList();
        if (dt.Rows.Count== 0)
        {
            return "NA";
        }
        else
        {
            return dt.Rows[0].ID.ToString();
        }

“System.Data.DataRow”不包含“ID”的定义,也没有扩展方法“ID”接受类型为“System.Data.DataRow”的第一个参数
可以找到(您是否缺少using指令或程序集引用?)

无法将类型'string'隐式转换为'System.Data.DataTable'

从'System.Collections.Generic.List'到'System.Data.DataTable'

最佳答案

更换

 Entities2 lg = new Entities2();
               List<SP_GetLogin_Result> gr = lg.SP_GetLogin(username, password).ToList();
                DataTable dt = new DataTable();
                dt.Columns.Add("id", typeof(string));
                foreach (var l in gr)
                {
                    dt.Rows.Add(l.id);


          }

通过
Entities2 lg = new Entities2();
     DataTable dt = new DataTable();
    dt = lg.SP_GetLogin(username, password);
    if(dt.rows.count == 0)
    {
    return "NA";
    }
    else
    {
    return dt.rows[0].something. tostring();
    }

尝试这个

关于asp.net - 登录不正确时显示消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39246749/

相关文章:

c# - 列表包含 linq 中的 ID

asp.net - 如果提交不应推送到存储库的文件夹,.hgignore 文件是否有效?

python - Python ValueError的可能原因

c# - HTML 按钮调用 ASP 类?

c# - 是否可以有一个可用于模型绑定(bind)的非公共(public)无参数构造函数?

php - 在ajax()方法jQuery中未收到错误消息

exception - 调用方法 'login' 时出现 Meteor OAuth 异常

forms - 使用SenchaTouch的简单登录表单

error-handling - 作业崩溃后复制输出

php - 如何正确处理 PDO 连接错误?