C# 用c在asp.net中使用windows身份验证#

C# 用c在asp.net中使用windows身份验证#,c#,asp.net,sql,windows-authentication,C#,Asp.net,Sql,Windows Authentication,我试图了解windows身份验证的工作原理以及如何实现它。我在youtube上读了不少文章,也看了一些很长的视频,但我仍然不知道需要向我的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.

我试图了解windows身份验证的工作原理以及如何实现它。我在youtube上读了不少文章,也看了一些很长的视频,但我仍然不知道需要向我的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文件

<?xml version="1.0"?>

<configuration>

  <connectionStrings>
    <add name="Asset management System DBConnectionString" connectionString="Data Source=STEPHENP\SQLEXPRESS;Initial Catalog=&quot;Asset management System DB&quot;;Integrated Security=True" providerName="System.Data.SqlClient"/>
  </connectionStrings>

  <system.web>

    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
        <add assembly="System.Web.Extensions.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add assembly="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
      </assemblies>
    </compilation>

    <authentication mode="Windows">
    </authentication>
    <identity impersonate="true"/>

  </system.web>

</configuration>

您混淆了SQL身份验证和Windows身份验证

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

<authentication mode="Windows">

Class.

您需要使用
authorization
元素锁定位置。您正在对数据库进行某种用户名和密码检查,这更像是基于表单的身份验证。Windows身份验证的目的是不具有此功能,或者如果需要,Windows身份验证将锁定可以访问登录页的用户。您已在web.config中正确放置了authentication元素,但缺少authorization元素。请参阅本页了解更多信息和理解。谢谢大家,我在web.config文件中添加了这一行,还有其他代码需要添加到index.aspx页面吗?我明白了,这是我第一次遇到安全问题,所以我正在尝试正确地执行此操作,但还是感到困惑,谢谢