使用c#在asp.net中使用Windows身份validation

我试图了解Windows身份validation的工作原理以及如何实现它。 我已阅读了不少文章,并在youtube上观看了一些相当长的video,但我仍然不知道需要添加到我的web.config文件/ index.aspx页面以使其正常工作。

这是index.aspx页面:

using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; using System.Configuration; using System.Data; namespace asset_management_system { public partial class index1 : System.Web.UI.Page { DataAccessLayer dal = new DataAccessLayer(); protected void Page_Load(object sender, EventArgs e) { } protected void loginBut_Click(object sender, EventArgs e) { string username = usernameTB.Text.Trim(); string password = passwordTB.Text.Trim(); try { using (SqlDataReader dr = dal.CheckLoginDetails(username)) { //if username does not exist if (!dr.Read()) { MessageBox.Show("Invalid login details"); } else { //if password matches the username then redirect to home page if (dr[0].ToString() == password) { Session["username"] = username; Response.Redirect("Home/home.aspx"); } else { MessageBox.Show("Invalid login details"); } } } } catch (SqlException sqlex) { MessageBox.Show("There may be an issue with the server, please contact the administrator" + " and provide this error message: " + sqlex); } catch (Exception ex) { MessageBox.Show("error message: " + ex); } }//end of loginBut_click method }//end of class }//end of namespace 

这是web.config文件

                   

您将SQL身份validation与Windows身份validation混淆。

为了使此网页基于Windows身份validation工作,您的web.config需要

  

将页面部署到Web服务器时,需要禁用匿名身份validation以限制外部用户。 以下是IIS7 + Web服务器身份validation部分的代码段:

在此处输入图像描述

在此处输入图像描述

如果需要针对登录用户或其组进行编程,则需要使用WindowsIdentity类。