在模拟中设置表单身份验证

时间:2016-03-07 06:03:19

标签: sql asp.net windows-authentication impersonation

我有一个内联网网站。我的数据库使用Windows身份验证我的网站使用logonuser()使用网络凭据。

IntPtr token = IntPtr.Zero;

        bool result = LogonUser(txtUsername.Text, txtDomain.Text, txtPassword.Text, 2, 0, ref token); //2, 0, ref token);
        if (result)
        {
            WindowsIdentity newId = new WindowsIdentity(token);
            WindowsImpersonationContext impersonatedUser = newId.Impersonate();
            FormsAuthentication.SetAuthCookie(txtDomain.Text + "\\" + txtUsername.Text, false);
            using (SqlConnection cnn=new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
            {
                using (SqlCommand cmd=new SqlCommand("select * from [user] where username=@user", cnn))
                {
                    cmd.Parameters.Clear();
                    cmd.Parameters.AddWithValue("@user",txtDomain.Text+"\\"+txtUsername.Text);

                    cnn.Open();
                    SqlDataReader rdr=cmd.ExecuteReader();
                    rdr.Read();
                    if(rdr.HasRows)
                    {   
                        cnn.Close();
                        lblNotification.Text = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
                        Session.Add("principal", new WindowsPrincipal(newId));
                    }
                    else
                    {
                        lblNotification.Text="Username or Password is Invalid";
                    }
                }
            }

代码执行正常,包括sql,但是当它进入下一页(home.aspx)时,我收到此错误:

error message

在sql中的Windows身份验证中遇到错误。 ' ECP'是我的域名' PHAPPCVTL002198 $'是我的电脑名称,但我没想到。我期待我在登录表单中输入的用户名。

0 个答案:

没有答案