Skip to content

Phanix's Blog

Menu
  • About Me / 關於我

.NET 取得 windows 帳號資訊

2011-06-20
| No Comments

取得登入帳號

Environment.UserName

取得帳號等有儲存在本機上的資訊

using System.Security.Principal;
using System.Threading;

//-----

AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);

WindowsPrincipal myPrincipal = (WindowsPrincipal)Thread.CurrentPrincipal;

WindowsIdentity myIdentity = (WindowsIdentity)myPrincipal.Identity;

Console.WriteLine("IdentityType: " + myIdentity.ToString());
Console.WriteLine("Name: {0}", myIdentity.Name);
Console.WriteLine("Member of Users? {0}", myPrincipal.IsInRole(WindowsBuiltInRole.User));
Console.WriteLine("Member of Administrators? {0}", myPrincipal.IsInRole(WindowsBuiltInRole.Administrator));
Console.WriteLine("Authenticated: {0}", myIdentity.IsAuthenticated);
Console.WriteLine("Anonymous: {0}", myIdentity.IsAnonymous);

取得帳號等有儲存在本機上的資訊 – II

使用 Win32 API

using System.Runtime.InteropServices;

//-----
[DllImport("Advapi32.dll", EntryPoint="GetUserName",  ExactSpelling=false, SetLastError=true)]
static extern bool GetUserName([MarshalAs(UnmanagedType.LPArray)] byte[] lpBuffer,
						[MarshalAs(UnmanagedType.LPArray)] Int32[] nSize );


//-----
byte[] str = new byte[256];
Int32[] len = new Int32[1];
len[0] = 256;
GetUserName(str, len);
MessageBox.Show(System.Text.Encoding.ASCII.GetString(str));

string a = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();

MessageBox.Show(a.ToString());

LDAP

這是可以取得最詳細資訊的方法

using System.DirectoryServices;

//-----

string principal = Environment.UserName;
string filter = string.Format("(&(ObjectClass={0})(sAMAccountName={1}))", "person", principal);
string[] properties = new string[] { "fullname" };

//用匿名登入,理論上LDAP都會允許,但只有 read 權限
DirectoryEntry adRoot = new DirectoryEntry("LDAP://yourdomain.com", null, null, AuthenticationTypes.Secure);
DirectorySearcher searcher = new DirectorySearcher(adRoot);
searcher.SearchScope = SearchScope.Subtree;
searcher.ReferralChasing = ReferralChasingOption.All;
searcher.PropertiesToLoad.AddRange(properties);
searcher.Filter = filter;
SearchResult result = searcher.FindOne();
DirectoryEntry directoryEntry = result.GetDirectoryEntry();

string displayName = directoryEntry.Properties["displayName"][0].ToString();
string firstName = directoryEntry.Properties["givenName"][0].ToString();
string lastName = directoryEntry.Properties["sn"][0].ToString();
string email = directoryEntry.Properties["mail"][0].ToString();
string department = directoryEntry.Properties["department"][0].ToString();
string description = directoryEntry.Properties["description"][0].ToString();

Share this:

  • Twitter
  • Facebook
  • Pinterest
學習工作, 工作, 程式
| Tags: .net, Account, c#, LDAP, programming, windows

Post navigation

Cycles Gladiator quick tasting
Last Summer in Vancouver – I

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Print date time with ms in PHP, which is faster?
  • Backup mongodb to GCP storage with crontab
  • Apache multiple allow origin (enable CORS) for multiple domains
  • Google Calendar 行事曆加行程 link
  • 從 GitLab 13.8.x 升級到 14.2.x

Recent Comments

  • 從 GitLab 13.8.x 升級到 14.2.x - Phanix's Blog on 從 GitLab 11.6.3 升級到 13.7.4
  • AB test to send HTTP POST/PUT with multipart data and headers - Phanix's Blog on Create HTTP PUT request to upload a file (c# & python)
  • Domaine de Bellene & Maison Roche de Bellene Tasting | Phanix's Blog on Nicolas Potel
  • Abruzzo wine Italy on Italian wine tasting
  • Janet on [不推薦食記] A**ki Burger

Archives

Tags

.net (28) 2008 (66) 2009 (91) Bordeaux (44) Bourgogne (35) c# (32) Cabernet Sauvignon (40) California (84) Chardonnay (46) dessert wine (24) food (53) France (52) France 法國 (68) French (28) French wine (22) japanese food (31) murmuring (25) photo-taking (46) photographing (48) Pinot Noir (51) programming (36) red wine (184) Santa Cruz (33) system administration (28) Taiwan (39) traveling (74) USA (124) white wine (116) wine (173) wine tasting (217) 加州 (63) 勃根地 (38) 台灣 (28) 品酒 (215) 攝影 (93) 旅遊 (60) 氣泡酒 (22) 法國 (27) 波爾多 (37) 甜酒 (26) 白酒 (112) 系統管理 (22) 紅酒 (181) 美國 (99) 義大利 (36)

Categories

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Phanix's Blog 2022 . Powered by WordPress