簡體   English   中英

C#中的LDAP和ActiveDirectory身份驗證

[英]LDAP and ActiveDirectory authentication in C#

我是LDAP和Active Directory身份驗證的新手,我只是研究了有關LDAp身份驗證的一些知識,並通過示例應用程序完成了

我只是檢查用戶是否存在於ActiveDirectory中

public static bool DoesUserExist()
  {
  using (var domainContext = new PrincipalContext(ContextType.Domain,Environment.UserDomainName))
   {
     using (var foundUser = UserPrincipal.FindByIdentity(domainContext, IdentityType.SamAccountName, Environment.UserName))
                {
                    return foundUser != null;
                }
            }
        }

在我們的本地系統中,它的工作正常,但是當我托管在ActiveDirectory Server中並且嘗試使用服務器IP地址訪問它時,我遇到了一些問題,例如

ContextType.Domain,Environment.UserDomainName and  Environment.UserName

因為這三個值來自服務器信息,而不是訪問此應用程序的用戶

因此,請幫助我如何獲取用戶信息(訪問此應用程序的用戶),以便我需要將這些信息傳遞給服務器,並需要檢查用戶是否為activedirectory用戶

Environment.UserDomainName返回域部分Environment.UserName ,如“mydomain.com”,所以你不希望出現這種情況。

Environment.UserName本身將返回當前“登錄Windows”的用戶,即應用程序池用戶-請參見MSDN

最好檢查當前Web請求的身份,因此在MVC控制器或WebForms頁面中,使用this.User

或者,如果您使用Windows身份驗證或將窗體身份驗證掛接到AD中,則當前Thread Principal應為當前請求用戶,因此可以使用Thread.CurrentPrincipal.Identity

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM