ASP.Net 获取登陆用户信息方法

来源:互联网 发布:多特软件站官方网站 编辑:程序博客网 时间:2024/06/05 11:56

工作需要,asp程序需要获取访问用户的相关信息。查阅大量资料,记录以收藏

 

1)通过office interop控件可以获取,但发现这种方法并不适用于server端(此方法可在之前帖子中找到)。

http://support.microsoft.com/default.aspx?scid=kb;en-us;257757

 

2)寻找过其他替代方法,但这些方法需要用户名和密码登陆通过OWA获取,这并不是在server端很好的办法,该方法可参阅:

1. http://weblogs.asp.net/whaggard/archive/2007/01/30/how-do-i-access-my-outlook-contacts-from-my-web-application.aspx

 

2. http://blogs.msdn.com/webdav_101/archive/2008/06/11/getting-a-list-of-mailboxes.aspx

 

 

3)通过Active Directory方式可以获取企业域内的相关信息,域内的服务器可以直接获取而不需要特定用户名密码登陆。

参阅:http://weblogs.asp.net/jpinquie/archive/2008/02/06/how-to-get-domain-user-information-from-active-directory-in-c.aspx

 

http://www.codeproject.com/KB/exchange/AccessGAL.aspx

 

主要实现:

using System.DirectoryServices;

 

 

 string filter = "(&(objectCategory=person)(SAMAccountName=alias))";

 DirectorySearcher searcher = new DirectorySearcher(filter);

 

foreach (SearchResult result in searcher.FindAll())
            {

                // Do work with data returned for each address entry

                DirectoryEntry entry = result.GetDirectoryEntry();

                Console.WriteLine("Name:  {0} {1}/nEmail: {2}/nPhone: {3}/nDisplayName:{4}",

                    entry.Properties["givenName"].Value,

                    entry.Properties["sn"].Value,

                    entry.Properties["mail"].Value,

                    entry.Properties["telephonenumber"].Value,

                    entry.Properties["displayname"].Value
                    );

 

//if the properties has more than 1 items, use the method below

                if (entry.Properties["memberof"].Count > 0)
                {
                    for (int i = 0; i < entry.Properties["memberof"].Count; i++)
                    {
                        string group = entry.Properties["memberof"][i].ToString();
                        if (group.StartsWith("CN="))
                        {
                            group = group.Substring(3, group.IndexOf(',') - 3);
                        }
                        //Console.WriteLine(entry.Properties["memberof"][i].ToString());
                        Console.WriteLine("======"+group+"=============");
                    }
                }