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();
學習工作, 工作, 程式
| 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 redirects specific site requests to the other URL
  • Apache multiple allow origin (enable CORS) for multiple domains
  • Google Calendar 行事曆加行程 link

Recent Comments

  • 天灰 on 撲克牌遊戲 — 德州撲克 (Texas Hold’em / Texas Poker)
  • 用FFmpeg取代 AWS Media Converter - Phanix's Blog on AWS S3+Media Converter+CloudFront 做 video file streaming CDN服務
  • Mount GCP storage as filesystem, and access with php - Phanix's Blog on php+apache 執行 sudo 命令出現 sudo: no tty present and no askpass program specified
  • Chateau de la Roche-aux-Moines (Nicolas Joly) Tasting - Phanix's Blog on Nicolas Joly 與他的生物動力法哲學
  • 安裝非預設版號 certbot apache plugin / installing certbot apache plugin with non-default version number - Phanix's Blog on AWS Route53 Geo dns with letsencrypt

Archives

Tags

.net (29) 2008 (66) 2009 (91) Bordeaux (45) Bourgogne (36) c# (33) Cabernet Sauvignon (41) California (85) Chardonnay (46) dessert wine (26) food (53) France (57) France 法國 (68) French (28) japanese food (32) murmuring (25) photo-taking (46) photographing (48) php (41) Pinot Noir (52) programming (36) red wine (187) Santa Cruz (33) system administration (68) Taiwan (40) traveling (74) USA (125) white wine (119) wine (174) wine tasting (223) 加州 (64) 勃根地 (39) 台灣 (29) 品酒 (220) 攝影 (93) 旅遊 (60) 氣泡酒 (24) 法國 (31) 波爾多 (37) 甜酒 (28) 白酒 (115) 系統管理 (48) 紅酒 (184) 美國 (100) 義大利 (36)

Categories

Meta

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

Phanix's Blog 2023 . Powered by WordPress