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

  • gitlab CI/CD 設定筆記
  • 跨網域的cookie與資料安全 / Cross domain cookie and data security
  • 從 GitLab 11.6.3 升級到 13.7.4
  • 關閉Twilio 視訊聊天室的同步問題 / Sync issue when closing Twilio video call room
  • Gitlab expose both http & https

Recent Comments

  • 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
  • slzzp on [食譜] 油封豬五花 Pork Belly Joint Confit

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) portrait (21) programming (36) red wine (184) Santa Cruz (33) Syrah (22) Taiwan (38) traveling (74) USA (124) white wine (116) wine (173) wine tasting (217) 加州 (63) 勃根地 (38) 台灣 (28) 品酒 (215) 攝影 (93) 旅遊 (60) 氣泡酒 (22) 法國 (27) 波爾多 (37) 甜酒 (26) 白酒 (112) 紅酒 (181) 美國 (99) 義大利 (36)

Categories

Meta

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

Phanix's Blog 2021 . Powered by WordPress