You probably know that, in order to retrieve the currently logged in user in ASP.Net, you would use this code snippet:
System.Web.HttpContext.Current.User.Identity.Name()
This returns the current user in the DOMAIN\USER format. But sometimes, all you want is the user name itself, without the Domain and slash. Here’s a function you can use to do this:
VB.Net: Protected Function ClearDomain(ByVal sItem As String) As String Dim sLoc As Integer = sItem.IndexOf("\") + 1 Dim sOutPut As String sOutPut = sItem.Substring(sLoc) Return sOutPut End Function
C#: protected string ClearDomain(string sItem) { int sLoc = (sItem.IndexOf("\\") + 1); string sOutPut; sOutPut = sItem.Substring(sLoc); return sOutPut; }
Then, to use this function, you would do something like this:
VB.Net: Dim usr as string usr=ClearDomain(System.Web.HttpContext.Current.User.ToString())
C#: string usr; usr = ClearDomain(System.Web.HttpContext.Current.User.ToString());
All Things DotNet Discussed – Winforms/ASP.Net/SharePoint/WPF
Leave a reply
You must be logged in to post a comment.