Context.User.Identity.Name Is Empty Null Blank

Context.User.Identity.Name Is Empty Null Or Blank In Asp.Net. If you have set the forms authentication and trying to display logged in user name in your asp.net web application by getting the user name using user.identity.name and this is blank or empty as displayed in image below.

Context.User.Identity.Name is Empty

Then u might have missed few configuration settings.

If yor are getting user identity name null or emplty when u try to get it to display user name in welcome message (for example) then probably you have allowed anonymous access to your site hence user.identity.name is null.

To avoid this try to write the code as mentioned below

First of all set authentication mode to forms authentication in web.config file




Now deny anonymous access to your site by adding below mentioned code in authorization section of web.config.






the ? represents anonymous user

Now write this code in Page_load to check the value of Identity name.

protected void Page_Load(object sender, EventArgs e)
    {
        if (HttpContext.Current.User != null)
        {
            if (HttpContext.Current.User.Identity.IsAuthenticated)
            {
                lblName.Text = HttpContext.Current.User.Identity.Name.ToUpper().ToString();

            }
                      
        
        }
        
    }

Context.User.Identity.Name is Blank

Now identity name is not blank as shown in picture.

You may also read more about Forms Authentication And FormsAuthenticationTicket.

If you like this post than join us or share

0 comments:

Find More Articles