9 Haziran 2016 Perşembe

WindowsIdentity Sınıfı

Giriş
Şu satırı dahil ederiz.
using System.Security.Principal.WindowsIdentity;
Constructor - İsim
İsmini bildiğimiz bir kullanıcıyı belirterek oluştururuz.
var admin = new WindowsIdentity(@"Administrator");
var princ = new WindowsPrincipal(admin);
System.Threading.Thread.CurrentPrincipal = princ;
Constructor - Token
Şöyle yaparız.
IntPtr token;
LogonUser("username", "domain", "password", LogonType.LOGON32_LOGON_BATCH, 
LogonProvider.LOGON32_PROVIDER_DEFAULT,ref token);
WindowsIdentity identity = new WindowsIdentity(token);
GetCurrent metodu
Nesneyi şöyle elde ederiz.
WindowsIdentity identity = Thread.CurrentPrincipal.Identity;
veya
WindowsIdentity identity = WindowsIdentity.GetCurrent();
IsAuthenticated ve IsAnonymous
Şöyle kullanırız.
identity.IsAuthenticated && !identity.IsAnonymous
Name Alanı
IIdentity.Name alanı ile aynı. Yine de örnek vermek istedim. Şöyle kullanırız.
Console.WriteLine("Current user: " + WindowsIdentity.GetCurrent().Name);
Domain ismi kısmını atmak için şöyle yaparız.
string userName =  WindowsIdentity.GetCurrent().Name;
int charindex = UserName.IndexOf(@"\"); //Index of the first "\"
userName = userName.Remove(0, charindex + 1);

Hiç yorum yok:

Yorum Gönder