3

我想开发一个 ASP.NET 应用程序,它可以检测用户登录窗口域。这些凭据将用于登录 ASP.NET 应用程序。

我怎样才能做到这一点?

谢谢!

4

5 回答 5

8

在 IIS 中,打开集成 Windows 身份验证,并在代码中,如果您使用:

Request.ServerVariables["LOGON_USER"]

它将返回登录用户的 Windows 用户名,即 MYDOMAIN\MYUSERNAME

于 2009-03-27T10:14:49.333 回答
1

这是我用来针对 Active Directory 进行身份验证的 C# 代码

using System;
using System.DirectoryServices;

namespace XYZcompany.Enterprise
{
  public class AuthenicationMgr
  {
    private static readonly int AD_ERR_LOGON_FAIL = -2147023570;
    private static readonly string _path = "LDAP://xxx.yyy.ggg";
    private static readonly string _domain = "xxx.yyy.ggg";

    public static bool IsAuthenticated(string username, string pwd)
    {
      bool authenticatedFlag = true;
      string domainAndUsername = _domain + "\\" + username;
      DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd);
      try
      {
        // Bind to the native AdsObject to force authentication.
        Object obj = entry.NativeObject;
        DirectorySearcher search = new DirectorySearcher(entry);

        search.Filter = "(SAMAccountName=" + username + ")";
        search.PropertiesToLoad.Add("cn");
        SearchResult result = search.FindOne();

        if (result == null)
        {
          authenticatedFlag = false;
        }
        else
        {
          authenticatedFlag = true;
        }
      }
      catch (System.Runtime.InteropServices.COMException ex)
      {
        if (ex.ErrorCode == AD_ERR_LOGON_FAIL)
        {
          authenticatedFlag = false;
        }
        else
        {
          throw new ApplicationException("Unable to authenticate user due to system error.", ex);
        }
      }
      return authenticatedFlag;
    }
  }
}
于 2009-03-27T08:52:17.770 回答
1

对于 ASP.net,您可能可以使用

HttpContext.Current.User.Identity

如果 IIS 配置正确(至少没有匿名登录)

于 2009-03-27T08:55:33.283 回答
0

您应该查看活动目录成员资格提供程序。它内置在 ASP.NET 中。

于 2009-03-27T08:36:45.437 回答
-1
System.Security.Principal.WindowsIdentity.GetCurrent().User.ToString
于 2009-03-27T08:36:30.273 回答