登录控制身份验证不使用用户定义的数据库 [英] login control authentication not working with user defined database

查看:100
本文介绍了登录控制身份验证不使用用户定义的数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用sql database验证登录控制。但它无法正常工作。我错过了什么。



这是我的代码

I am trying to authenticate login control with sql database.But its not working.What am i missing.

Here is my code

private bool Userlogin(string un, string pw)
{
    SqlConnection con = new SqlConnection(constr);
    SqlCommand cmd=new SqlCommand("Select *from UserData where Username= @un and password=@pw",con );
    cmd.Parameters.AddWithValue("@un",un);
    cmd.Parameters.AddWithValue("@pw",pw);
    con.Open();
    string result=Convert.ToString(cmd.ExecuteScalar());
    if (string.IsNullOrEmpty(result)) return false;return true;
}
protected void login1_Authenticate(object sender, AuthenticateEventArgs e)
{
    string un = login1.UserName;
    string pw = login1.Password;
    bool result = Userlogin(un, pw);
    if (result)
    {
        e.Authenticated = true;
        Session["username"] = un;
    }
    else
    {
        e.Authenticated = false;
    }
}

推荐答案

在下面试试

try below
protected void login1_Authenticate(object sender, AuthenticateEventArgs e)
{
    string un = login1.UserName;
    string pw = login1.Password;
    bool result = Userlogin(un, pw);
    if (result)
    {
        FormsAuthentication.SetAuthCookie(un, true);
        e.Authenticated = true;
        Session["username"] = un;
    }
    else
    {
        e.Authenticated = false;
    }
}


这篇关于登录控制身份验证不使用用户定义的数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆