Published on: 22 Jul, 2013

How to get the current login user's username in ASP.Net C#?

Tumblr Digg Google Plus

To retrieve a current username of a login user is pretty simple in ASP.Net. Firstly you need to use the framework object of Membership In System.Net.Security.

Please see below quick code snippet on how to get the current username, if the user has not login on the site, it will return an error message.

private string GetCurrentUsername() {
    System.Web.Security.MembershipUser objCurrentUser = System.Web.Security.Membership.GetUser();
    if (objCurrentUser != null) {
        return objCurrentUser.UserName;
    } else {
        return "Error: user is not login on the site";
    }
}

Comments

There are no comments available.

Write Comment
0 characters entered. Maximum characters allowed are 1000 characters.