簡體   English   中英

在ASP.NET Web窗體IIS中獲取Windows登錄用戶

[英]Get windows logon user in asp.net web forms iis

我想以asp.net Web形式獲取活動目錄登錄用戶。我使用了以下代碼段,但在運行時或iis上均不起作用。

Request.ServerVariables["REMOTE_USER"].ToString();

嘗試使用System.Web.HttpContext.Current.User.Identity; 獲取經過身份驗證的用戶的詳細信息。

使用目錄條目獲取用戶狀態。

string username = "";
string userpassword = "";
bool valid = false;
using (DirectoryEntry Direntry = new DirectoryEntry(path, username, userpassword))   
  {
      using (DirectorySearcher Dsearch = new DirectorySearcher(Direntry))
            {
                Dsearch.Filter = "(cn=" + username + ")";
                try
                {
                    SearchResult adsSearchResult = Dsearch.FindOne();
                    if (adsSearchResult != null)
                    {
                        valid = true;
                    }   
                }
                catch (Exception ex)
                {

                }
                finally
                {
                    Direntry.Close();
                }
            }
        }

您可能需要進行的另一項更改是在web.config文件中。

將身份驗證模式從窗體更改為Windows。

 <authentication mode="Windows"/> 

文獻資料

暫無
暫無

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

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