c# - 登录错误消息对象引用

标签 c# asp.net login error-handling

我有两个页面,分别称为Login.aspxHome.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("home.aspx");
            }
            else if (Session["ID"] != null)
            {

                    u = Session["ID"].ToString();  
            }

            else
            {
                Label1.Text = ("Incorrect UserName/Password");

                Label1.Visible = true;
                Response.Redirect("home.aspx");
            }
            txt_us.Text = "";
            txt_pwd.Text = "";
    }

    private string loginmethod(string UserName, string Password)
    {
        try
        {
            login_class lg_class = new login_class();
            Entities login = new Entities();
            string logn = Convert.ToString(lg_class.loginfunction(UserName, Password).Rows[0]["id"]);
            Session["ID"] = logn.ToString();
            return (logn);
        }
        catch (Exception ex)
        {            
            return (ex.Message.ToString());            
        }
    }

    private string Assignroles(string username)
    {
        if ((txt_us.Text != string.Empty) && (txt_pwd.Text != string.Empty))
            return "";
        else
            return string.Empty;
    }

    public DataTable loginfunction(string username, string password)
    {
        try
        {
            Entities lg = new Entities();
            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();
        }
    }

并在home.aspx页面上加载:
    string id;

    protected void Page_Load(object sender, EventArgs e)
    {          
        if(!IsPostBack)
        {   
            id = Session["ID"].ToString();
           this.FFID = "All";
            vehcileinfo(id);                     
        }    
    }
SP_GetLogin返回ID,即当我输入用户名abc和pssword 1224时,此返回ID CU-2343
当我使用正确的用户名和密码输入时,此重定向到home.aspx
但是当我输入错误的用户名或密码时,这会在此行显示错误
u = Session["ID"].ToString();

System.NullReferenceException: Object reference not set to an instance of an object.



我想在login.aspx上显示错误消息
“错误的用户名和密码”

我该怎么做?

最佳答案

根据评论中的讨论,获取NullReferenceException的异常(exception)

这意味着,您正在尝试使用空值。这意味着您要么将其设置为null,要么根本不将其设置为任何值。

因此,只需检查Home.aspx的Page_Load

if(Session["ID"] != null)
{ 
   id = Session["ID"].ToString();
} 

else
{
  // Do logic here
} 

关于c# - 登录错误消息对象引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39674026/

相关文章:

c# - Linq 比较 2 个对象列表

ruby-on-rails - 如何将登录表单放在 Devise 和 Rails 的标题中

c# - ASP.NET MVC 4 和 WYSIWYG 编辑器验证

asp.net - 如何以ASP.NET形式使用gmail SMTP

django - 为什么authenticate() 为非事件用户返回None?

login - JBoss Wildfly - 数据库登录模块

c# - IEnumerable<T>.Count 在哪些情况下进行了优化?

c# - 在 UWP 应用程序上滚动时禁用枢轴滑动

c# - 独立运行 Silverlight 3

c# - 如何在ASP.NET Web API中使用非线程安全的异步/等待API和模式?