Error coming while hosting ASP.NET Web application in windows server

BeUnique 2,332 Reputation points
2021-12-06T17:05:14.433+00:00

I am developing ASP.NET Application. It's working in my local system.

But, when i host in server, it's throwing error in login page.

Error : Input string was not in a correct format.

below is my code in page_load. what is the problem.

 string rUser;
                    string rDomain;
                    string User;
                    int Ruser_Number;
                    rUser = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
                    rDomain = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
                    rDomain = rDomain.Substring(0, ((rDomain.IndexOf("\\") + 1) - 1));
                    rUser = rUser.Substring((rUser.IndexOf("\\") + 1), (rUser.Length - (rUser.IndexOf("\\") + 1)));
                    //Get Windows User List
                    HttpContext.Current.Session["WindowsId"] = rUser;
Windows development Internet Information Services
Developer technologies ASP.NET Other
0 comments No comments
{count} votes

Accepted answer
  1. Bruce Zhang-MSFT 3,771 Reputation points
    2021-12-07T07:32:30.65+00:00

    Hi @BeUnique ,

    i am getting error in the below line.

    var tokens = fullName.Split('\', 2);

    2 is not necessary, so you can remove it and it won't affect the domain nad userName.
    155576-1.jpg
    155606-2.jpg

    I also test your initial code, it works well. How did you know the error caused by it, did YSOD return that? Can you show the error page? What's your environment and IIS or application configuration?
    155566-3.jpg
    155510-4.jpg


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,
    Bruce Zhang

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Michael Taylor 60,161 Reputation points
    2021-12-06T17:26:16.92+00:00

    It would be useful if you could identify which line is causing the issue. But taking a quick look at your code it looks like you're building a Windows authenticated site and trying to get the current user's domain and user name. Unfortunately your code is a little too complex and this is part of the problem.

       var fullName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;  
       var tokens = fullName.Split('\\', 2);  
         
       var domain = tokens.Length > 1 ? tokens[0] : "";  
       var userName = (tokens.Length > 1) ? tokens[1] : tokens[0];  
    

    The other aspect of this is that you need to ensure that you have configured your site in IIS to use Windows auth and not Forms auth or Anonymous. Otherwise the user you are getting is going to be the Windows account that is running the process and not the user accessing your site.

    I also question why you're bothering to store any of this in session at all. The HttpContext already exposes the current user via the HttpContext.User property. You can use that to get just the name as needed via an extension method.

       public static class SecurityExtensions  
       {  
           public static string GetUserName ( this System.Security.Principal.IPrincipal user )  
           {              
               var fullName = user?.Identity?.Name ?? "";  
               var tokens = fullName.Split('\\', 2);  
         
               return (tokens.Length > 1) ? tokens[1] : tokens[0];  
           }  
       }  
         
       //And then later in your code (I'm assuming an MVC controller here)  
       void SomeWebMethod ()  
       {  
          var name = User.GetUserName();  
       }  
    

  2. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2021-12-07T19:57:32.963+00:00

    you are getting the name of service account that is running your site. it is typically a local account so there is no "\" in the name.

    so the correct code is:

    var  names = System.Security.Principal.WindowsIdentity.GetCurrent().Name.Split('\\');
    
    if (names.length < 2)
    {
       rDomain = "";  //or what default value you want
       rUser = names[0];
    }
    else
    {
       rDomain = names[0];
       rUser = names[1];
    }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.