简体   繁体   中英

Get windows logon user in asp.net web forms iis

我想以asp.net Web形式获取活动目录登录用户。我使用了以下代码段,但在运行时或iis上均不起作用。

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

Try using System.Web.HttpContext.Current.User.Identity; to get details of the authenticated user.

Use Directory Entry to get the user status.

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();
                }
            }
        }

One additional change you may need to make is in the web.config file.

Change the authentication mode from Forms to Windows.

 <authentication mode="Windows"/> 

Documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM