public partial class Default : System.Web.UI.Page { protected void Page_Load( object sender, EventArgs e) { var domainUserName = System.Web.HttpContext.Current.User.Identity.Name; var authenticationType = System.Web.HttpContext.Current.User.Identity.AuthenticationType; Response.Write( "域账号:" + domainUserName + "<br/>" ); Response.Write( "认证类型:" + authenticationType + "<br/>" ); var user = this .GetUserInfo(domainUserName); if (user != null ) { Response.Write( "登录名:" + user.SAMAccountName + "<br/>" ); Response.Write( "短名称:" + user.GivenName + "<br/>" ); Response.Write( "名称:" + user.CN + "<br/>" ); Response.Write( "邮件:" + user.Email + "<br/>" ); } } private UserInfo GetUserInfo( string domainUserName) { try { if ( string .IsNullOrEmpty(domainUserName)) { return null ; } var userArr = domainUserName.Split( '\\' ); var domain = userArr[0]; var loginName = userArr[1]; var entry = new DirectoryEntry( string .Concat( "LDAP://" , domain)); var search = new DirectorySearcher(entry); search.Filter = string .Format( "(SAMAccountName={0})" , loginName); search.PropertiesToLoad.Add( "SAMAccountName" ); search.PropertiesToLoad.Add( "givenName" ); search.PropertiesToLoad.Add( "cn" ); search.PropertiesToLoad.Add( "mail" ); var result = search.FindOne(); if (result != null ) { var info = new UserInfo(); info.SAMAccountName = result.Properties[ "SAMAccountName" ][0].ToString(); info.GivenName = result.Properties[ "givenName" ][0].ToString(); info.CN = result.Properties[ "cn" ][0].ToString(); info.Email = result.Properties[ "mail" ][0].ToString(); return info; } } catch { } return null ; } public sealed class UserInfo { public string SAMAccountName; public string GivenName; public string CN; public string Email; } } |