aspx Tutorial

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

Toolbar

Get our toolbar!

Advertize



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

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

C#

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();

    }

}

 

VB.NET

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="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

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

    <div>

        <h2>

            Current Window Username:

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

        </h2>

    </div>

    </form>

</body>

</html>

 

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

 

C#

getDomainName function will return domain name.

    public String getDomainName(string Name)

    {

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

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

    }

 

VB.NET

 

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

End Function

 

C#


geUserName function will return user name.

    public String geUserName(string Name)

    {

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

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

    }

 

VB.NET

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.


Output

Download

CurrentUser.rar (770.00 bytes)

Protected by Copyscape Online Plagiarism Tool

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Advertizement 1
Advertizement 2
Advertizement 3
Advertizement 4
Advertizement 5