aspx Tutorial

NET Articles,jQuery demo, with jQuery, online tutorial,Jquery, SilverLight, Javascript,,JSON, MVC,.NET Articles,demo, Web Services,
Advertise Here


Get our toolbar!


Posted by Aamir Hasan   on Tuesday, June 14, 2011 Total Views:  

In this example, you will see how to get current windows username in
Here’s an example.
Code behind script 


using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;


public partial class Current_UserName : System.Web.UI.Page


    protected void Page_Load(object sender, EventArgs e)


        lUserName.Text = CurrentUserName();



    public String CurrentUserName()


        return System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();





Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Public Partial Class Current_UserName
   Inherits System.Web.UI.Page

   Protected Sub Page_Load(sender As Object, e As EventArgs)
       lUserName.Text = CurrentUserName()
   End Sub

   Public Function CurrentUserName() As [String]
       Return System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString()
   End Function
End Class



Degin script


<html xmlns="">

<head runat="server">




    <form id="form1" runat="server">



            Current Window Username:

            <asp:Label ID="lUserName" runat="server" Text="Label"></asp:Label>







Above function will return domain name and username. For example, aspxtutorial\aamirhasan



getDomainName function will return domain name.

    public String getDomainName(string Name)


        return Name.Contains("\\") ? (Name.Split(new string[] { "\\" },

                                     StringSplitOptions.None)[0].ToString()) : (null);





Public Function getDomainName(Name As String) As [String]
	Return If(Name.Contains("\"), (Name.Split(New String() {"\"}, StringSplitOptions.None)(0).ToString()), (Nothing))

End Function



geUserName function will return user name.

    public String geUserName(string Name)


        return Name.Contains("\\") ? (Name.Split(new string[] { "\\" },

                                      StringSplitOptions.None)[1].ToString()) : (null);




Public Function geUserName(Name As String) As [String]
	Return If(Name.Contains("\"), (Name.Split(New String() {"\"}, StringSplitOptions.None)(1).ToString()), (Nothing))

End Function


Note: Make sure your application is integrated Windows authentication.



CurrentUser.rar (770.00 bytes)

Protected by Copyscape Online Plagiarism Tool

Add comment

  Country flag

  • Comment
  • Preview

Advertizement 1
Advertizement 2
Advertizement 3
Advertizement 4
Advertizement 5