【问题标题】:Using windows authentication in asp.net with c#使用 c# 在 asp.net 中使用 windows 身份验证
【发布时间】:2013-08-05 15:21:11
【问题描述】:

我试图了解 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>

【问题讨论】:

  • 您需要使用authorization 元素锁定位置。
  • 您正在对数据库中的用户名和密码进行某种检查,这更像是一种基于表单的身份验证。 Windows 身份验证的目的是没有那个,或者如果您需要 Windows 身份验证将锁定谁可以访问登录页面。您已在 web.config 中正确放置了身份验证元素,但缺少授权元素。请参阅此页面了解和了解。 msdn.microsoft.com/en-us/library/8d82143t(v=vs.85).aspx
  • 谢谢大家,我在 web.config 文件中添加了这一行 是否有其他代码我应该添加到我的 index.aspx 页面?

标签: c# asp.net sql windows-authentication


【解决方案1】:

您将 SQL 身份验证与 Windows 身份验证混淆了。

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

<authentication mode="Windows">

当您将页面部署到 Web 服务器时,您需要禁用匿名身份验证以限制外部用户。下面是来自 IIS7+ Web 服务器的身份验证部分的 sn-p:

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

【讨论】:

  • 我明白了,这是我第一次遇到安全问题,所以我试着做对了,但感到困惑,谢谢
猜你喜欢
  • 2016-08-25
  • 2017-04-17
  • 2019-01-28
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
  • 2011-06-27
  • 2010-11-27
  • 2011-07-17
相关资源
最近更新 更多