Here you will find .Net source code, for ASP.Net, WinForms, WCF, Unit Testing, Security and more...
If you find this code useful, please visit our sponsers who help keep this site running.
Tip: Turn off line numbers when using Copy and Paste.
1using System.Security.Principal; 2using System.ServiceModel; 3using System.Web; 4 5namespace Enterprise.Security 6{ 7 /// <!-- 8 /// Code Courtesy Of http://www.purecoding.net 9 /// Permissions : Free for general use 10 /// Keywords: Security, Identity, WCF, c#, .Net, Windows, Aspnet. 11 /// Filename: IdentityManager.cs 12 /// --> 13 /// <summary> 14 /// Gets the context of the calling user. 15 /// Works with WCF Services, Xml WebServices and ASPNet 16 /// </summary> 17 public static class IdentityManager 18 { 19 public static IIdentity CurrentUser() 20 { 21 if (HttpContext.Current == null && ServiceSecurityContext.Current != null) 22 { 23 return ServiceSecurityContext.Current.PrimaryIdentity; 24 } 25 else if (HttpContext.Current != null) 26 { 27 return HttpContext.Current.User.Identity; 28 } 29 else 30 { 31 return System.Security.Principal.WindowsIdentity.GetCurrent(); 32 } 33 } 34 } 35} 36