日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

ad 单点登录 java 访问权限_AD 单点登录以及windows认证详细说明

發布時間:2023/12/2 windows 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ad 单点登录 java 访问权限_AD 单点登录以及windows认证详细说明 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

上篇博客我談到了一些關于ASP.NET Forms身份認證方面的話題,這次的博客將主要介紹ASP.NET Windows身份認證。

Forms身份認證雖然使用廣泛,不過,如果是在 Windows Active Directory 的環境中使用ASP.NET, 那么使用Windows身份認證也會比較方便。 方便性表現為:我們不用再設計登錄頁面,不用編寫登錄驗證邏輯。而且使用Windows身份認證會有更好的安全保障。

認識ASP.NET Windows身份認證

要使用Windows身份認證模式,需要在web.config設置:

Windows身份認證做為ASP.NET的默認認證方式,與Forms身份認證在許多基礎方面是一樣的。上篇博客我說過:我認為ASP.NET的身份認證的最核心部分其實就是HttpContext.User這個屬性所指向的對象。在接下來的部分,我將著重分析這個對象在二種身份認證中有什么差別。

在ASP.NET身份認證過程中,IPrincipal和IIdentity這二個接口有著非常重要的作用。 前者定義用戶對象的基本功能,后者定義標識對象的基本功能, 不同的身份認證方式得到的這二個接口的實例也是不同的。

ASP.NET Windows身份認證是由WindowsAuthenticationModule實現的。WindowsAuthenticationModule在ASP.NET管線的AuthenticateRequest事件中, 使用從IIS傳遞到ASP.NET的Windows訪問令牌(Token)創建一個WindowsIdentity對象,Token通過調用context.WorkerRequest.GetUserToken()獲得, 然后再根據WindowsIdentity 對象創建WindowsPrincipal對象, 然后把它賦值給HttpContext.User。

在Forms身份認證中,我們需要創建登錄頁面,讓用戶提交用戶名和密碼,然后檢查用戶名和密碼的正確性, 接下來創建一個包含FormsAuthenticationTicket對象的登錄Cookie供后續請求使用。FormsAuthenticationModule在ASP.NET管線的AuthenticateRequest事件中, 解析登錄Cookie并創建一個包含FormsIdentity的GenericPrincipal對象, 然后把它賦值給HttpContext.User。

上面二段話簡單了概括了二種身份認證方式的工作方式。

我們可以發現它們存在以下差別:

1. Forms身份認證需要Cookie表示登錄狀態,Windows身份認證則依賴于IIS

2. Windows身份認證不需要我們設計登錄頁面,不用編寫登錄驗證邏輯,因此更容易使用。

在授權階段,UrlAuthorizationModule仍然會根據當前用戶檢查將要訪問的資源是否得到許可。 接下來,FileAuthorizationModule檢查 HttpContext.User.Identity 屬性中的 IIdentity 對象是否是 WindowsIdentity 類的一個實例。 如果 IIdentity 對象不是 WindowsIdentity 類的一個實例,則 FileAuthorizationModule 類停止處理。 如果存在 WindowsIdentity 類的一個實例,則 FileAuthorizationModule 類調用 AccessCheck Win32 函數(通過 P/Invoke) 來確定是否授權經過身份驗證的客戶端訪問請求的文件。 如果該文件的安全描述符的隨機訪問控制列表 (DACL) 中至少包含一個 Read 訪問控制項 (ACE),則允許該請求繼續。 否則,FileAuthorizationModule 類調用 HttpApplication.CompleteRequest 方法并將狀態碼 401 返回到客戶端。

在Windows身份認證中,驗證工作主要是由IIS實現的,WindowsAuthenticationModule其實只是負責創建WindowsPrincipal和WindowsIdentity而已。 順便介紹一下:Windows 身份驗證又分為“NTLM 身份驗證”和“Kerberos v5 身份驗證”二種, 關于這二種Windows身份認證的更多說明可查看MSDN技術文章:解釋:ASP.NET 2.0 中的 Windows 身份驗證。 在我看來,IIS最終使用哪種Windows身份認證方式并不影響我們的開發過程,因此本文不會討論這個話題。

根據我的實際經驗來看,使用Windows身份認證時,主要的開發工作將是根據登錄名從Active Directory獲取用戶信息。 因為,此時不需要我們再設計登錄過程,IIS與ASP.NET已經為我們準備好了WindowsPrincipal和WindowsIdentity這二個與用戶身份相關的對象。

訪問 Active Directory

我們通常使用LDAP協議來訪問Active Directory, 在.net framework中提供了DirectoryEntry和DirectorySearcher這二個類型讓我們可以方便地從托管代碼中訪問 Active Directory 域服務。

如果我們要在"test.corp”這個域中搜索某個用戶信息,我們可以使用下面的語句構造一個DirectoryEntry對象:DirectoryEntryentry =newDirectoryEntry("LDAP://test.corp");

在這段代碼中,我采用硬編碼的方式把域名寫進了代碼。

我們如何知道當前電腦所使用的是哪個域名呢?

答案是:查看“我的電腦”的屬性對話框:

注意:這個域名不一定與System.Environment.UserDomainName相同。

除了可以查看“我的電腦”的屬性對話框外,我們還可以使用代碼的方式獲取當前電腦所使用的域名:private static stringGetDomainName()

{

// 注意:這段代碼需要在Windows XP及較新版本的操作系統中才能正常運行。SelectQueryquery =newSelectQuery("Win32_ComputerSystem");

using( ManagementObjectSearchersearcher =newManagementObjectSearcher(query) ) {

foreach( ManagementObjectmo insearcher.Get() ) {

if( (bool)mo["partofdomain"] )

returnmo["domain"].ToString();

}

}

return null;

}

當構造了DirectorySearcher對象后,我們便可以使用DirectorySearcher來執行對Active Directory的搜索。

我們可以使用下面的步驟來執行搜索:

1. 設置 DirectorySearcher.Filter 指示LDAP格式篩選器,這是一個字符串。

2. 多次調用PropertiesToLoad.Add() 設置搜索過程中要檢索的屬性列表。

3. 調用FindOne() 方法獲取搜索結果。

下面的代碼演示了如何從Active Directory中搜索登錄名為“fl45”的用戶信息:static voidMain(string[] args)

{

Console.WriteLine(Environment.UserDomainName);

Console.WriteLine(Environment.UserName);

Console.WriteLine("------------------------------------------------");

ShowUserInfo("fl45", GetDomainName());

}

private static stringAllProperties ="name,givenName,samaccountname,mail";

public static voidShowUserInfo(stringloginName, stringdomainName)

{

if( string.IsNullOrEmpty(loginName) ||string.IsNullOrEmpty(domainName) )

return;

string[] properties =AllProperties.Split(new char[] { '\r', '\n', ','},

StringSplitOptions.RemoveEmptyEntries);

try{

DirectoryEntryentry =newDirectoryEntry("LDAP://"+domainName);

DirectorySearchersearch =newDirectorySearcher(entry);

search.Filter ="(samaccountname="+loginName +")";

foreach( stringp inproperties )

search.PropertiesToLoad.Add(p);

SearchResultresult =search.FindOne();

if( result !=null) {

foreach( stringp inproperties ) {

ResultPropertyValueCollectioncollection =result.Properties[p];

for( inti =0; i

Console.WriteLine(p +": "+collection[i]);

}

}

}

catch( Exceptionex ) {

Console.WriteLine(ex.ToString());

}

}

結果如下:

在前面的代碼,我在搜索Active Directory時,只搜索了"name,givenName,samaccountname,mail"這4個屬性。 然而,LDAP還支持更多的屬性,我們可以使用下面的代碼查看更多的用戶信息:private static stringAllProperties =@"

homemdb

distinguishedname

countrycode

cn

lastlogoff

mailnickname

dscorepropagationdata

msexchhomeservername

msexchmailboxsecuritydescriptor

msexchalobjectversion

usncreated

objectguid

whenchanged

memberof

msexchuseraccountcontrol

accountexpires

displayname

primarygroupid

badpwdcount

objectclass

instancetype

objectcategory

samaccounttype

whencreated

lastlogon

useraccountcontrol

physicaldeliveryofficename

samaccountname

usercertificate

givenname

mail

userparameters

adspath

homemta

msexchmailboxguid

pwdlastset

logoncount

codepage

name

usnchanged

legacyexchangedn

proxyaddresses

department

userprincipalname

badpasswordtime

objectsid

sn

mdbusedefaults

telephonenumber

showinaddressbook

msexchpoliciesincluded

textencodedoraddress

lastlogontimestamp

company

";

在ASP.NET中訪問Active Directory

前面我在一個控制臺程序中演示了訪問Active Directory的方法,通過示例我們可以看到:在代碼中,我用Environment.UserName就可以得到當前用戶的登錄名。 然而,如果是在ASP.NET程序中,訪問Environment.UserName就很有可能得不到真正用戶登錄名。 因為:Environment.UserName是使用WIN32API中的GetUserName獲取線程相關的用戶名,但ASP.NET運行在IIS中,線程相關的用戶名就不一定是客戶端的用戶名了。 不過,ASP.NET可以模擬用戶方式運行,通過這種方式才可以得到正確的結果。關于“模擬”的話題在本文的后面部分有說明。

在ASP.NET中,為了能可靠的獲取登錄用戶的登錄名,我們可以使用下面的代碼:///

///根據指定的HttpContext對象,獲取登錄名。///

///

/// public static stringGetUserLoginName(HttpContextcontext)

{

if( context ==null)

return null;

if( context.Request.IsAuthenticated ==false)

return null;

stringuserName =context.User.Identity.Name;

// 此時userName的格式為:UserDomainName\LoginName

// 我們只需要后面的LoginName就可以了。string[] array =userName.Split(new char[] { '\\'}, StringSplitOptions.RemoveEmptyEntries);

if( array.Length ==2)

returnarray[1];

return null;

}

在ASP.NET中使用Windows身份認證時,IIS和WindowsAuthenticationModule已經做了許多驗證用戶的相關工作, 雖然我們可以使用前面的代碼獲取到用戶的登錄名,但用戶的其它信息即需要我們自己來獲取。 在實際使用Windows身份認證時,我們要做的事:基本上就是從Active Directory中根據用戶的登錄名獲取所需的各種信息。

比如:我的程序在運行時,還需要使用以下與用戶相關的信息:public sealed classUserInfo{

public stringGivenName;

public stringFullName;

public stringEmail;

}

那么,我們可以使用這樣的代碼來獲取所需的用戶信息:public static classUserHelper{

///

///活動目錄中的搜索路徑,也可根據實際情況來修改這個值。/// public static stringDirectoryPath ="LDAP://"+GetDomainName();

///

///獲取與指定HttpContext相關的用戶信息///

///

/// public staticUserInfoGetCurrentUserInfo(HttpContextcontext)

{

stringloginName =GetUserLoginName(context);

if( string.IsNullOrEmpty(loginName) )

return null;

returnGetUserInfoByLoginName(loginName);

}

///

///根據指定的HttpContext對象,獲取登錄名。///

///

/// public static stringGetUserLoginName(HttpContextcontext)

{

if( context ==null)

return null;

if( context.Request.IsAuthenticated ==false)

return null;

stringuserName =context.User.Identity.Name;

// 此時userName的格式為:UserDomainName\LoginName

// 我們只需要后面的LoginName就可以了。string[] array =userName.Split(new char[] { '\\'}, StringSplitOptions.RemoveEmptyEntries);

if( array.Length ==2)

returnarray[1];

return null;

}

///

///根據登錄名查詢活動目錄,獲取用戶信息。///

///

/// public staticUserInfoGetUserInfoByLoginName(stringloginName)

{

if( string.IsNullOrEmpty(loginName) )

return null;

// 下面的代碼將根據登錄名查詢用戶在AD中的信息。

// 為了提高性能,可以在此處增加一個緩存容器(Dictionary or Hashtable)。try{

DirectoryEntryentry =newDirectoryEntry(DirectoryPath);

DirectorySearchersearch =newDirectorySearcher(entry);

search.Filter ="(SAMAccountName="+loginName +")";

search.PropertiesToLoad.Add("givenName");

search.PropertiesToLoad.Add("cn");

search.PropertiesToLoad.Add("mail");

// 如果還需要從AD中獲取其它的用戶信息,請參考ActiveDirectoryDEMOSearchResultresult =search.FindOne();

if( result !=null) {

UserInfoinfo =newUserInfo();

info.GivenName =result.Properties["givenName"][0].ToString();

info.FullName =result.Properties["cn"][0].ToString();

info.Email =result.Properties["mail"][0].ToString();

returninfo;

}

}

catch{

// 如果需要記錄異常,請在此處添加代碼。}

return null;

}

private static stringGetDomainName()

{

// 注意:這段代碼需要在Windows XP及較新版本的操作系統中才能正常運行。SelectQueryquery =newSelectQuery("Win32_ComputerSystem");

using( ManagementObjectSearchersearcher =newManagementObjectSearcher(query) ) {

foreach( ManagementObjectmo insearcher.Get() ) {

if( (bool)mo["partofdomain"] )

returnmo["domain"].ToString();

}

}

return null;

}

}

使用UserHelper的頁面代碼:

WindowsAuthentication DEMO - http://www.cnblogs.com/fish-li/
用戶短名:
用戶全名:
郵箱地址:

當前用戶還未登錄。

總結

以上是生活随笔為你收集整理的ad 单点登录 java 访问权限_AD 单点登录以及windows认证详细说明的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。