IMG-LOGO

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

andy - 22 Jul, 2013 3042 Views 0 Comment

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.

Related Articles

How to remove html tags from string in c#?

Sometimes you need to remove HTML tags from string to ensure there are no dangerous or malicious scripts especially when you want to store the string or data text into the database.