Solved

how to avoid errors when specifying a domain name in code and then changing it.

Posted on 2012-03-20
8
296 Views
Last Modified: 2012-03-20
hello,

I have a piece of code that retrieves the network user name, the code looks from the domain name to the user name, before it just gets the user name.

recently we went through a rename of our domain name, and I had to change the hard coded domain name in several projects.

I would like to know how to modify the code to avoid this situation in the future.

This is the code I have been using:

m_CurrentUser = HttpContext.Current.User.Identity.Name.ToString();
m_CurrentUser = m_CurrentUser.ToUpper().Replace("\\", "").Replace("CompanyDomain", "").ToLower();

Open in new window



Thank you!
0
Comment
Question by:metropia
  • 4
  • 3
8 Comments
 
LVL 38

Accepted Solution

by:
PaulHews earned 350 total points
ID: 37743494
This is a value that should be placed in a configuration file.

You can use the asp.net web.config file to store settings:
appSettings:
http://msdn.microsoft.com/en-us/library/ms228154.aspx

<appSettings>
   <add key="CompanyDomain" value="xyzzyz" />
</appSettings>

Open in new window


Retrieve:
string domainString =System.Configuration.ConfigurationManager.AppSettings["CompanyDomain"].ToString();

Open in new window

0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 150 total points
ID: 37743636
You can also do a simple split on \ (assuming the username is in format "DomainName\UserName") and then take the second element as username

Dim user as string = username.split("\")(1)
0
 

Author Comment

by:metropia
ID: 37744003
Is it possible to make the changes using some of the code I have, or do I have to change all that?

Thank you.
0
 
LVL 38

Assisted Solution

by:PaulHews
PaulHews earned 350 total points
ID: 37744087
Ideally, wherever you've hard-coded a value, you replace it with a variable that you populate from settings.  Typically this means editing your code, as in the sample below, not replacing it.   You just want to avoid a situation where you have to recompile just because of a change in the environment.

m_CurrentUser = HttpContext.Current.User.Identity.Name.ToString();
string domainString =System.Configuration.ConfigurationManager.AppSettings["CompanyDomain"].ToString();
m_CurrentUser = m_CurrentUser.ToUpper().Replace("\\", "").Replace(domainString, "").ToLower();

Open in new window

                                 

If you can get away without specifying the hard coded content, as in CodeCruiser's suggestion above, then it's sensible to do so.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:metropia
ID: 37744236
I like CodeCruiser's suggestion, but I am not sure how to implement it with my code.

If I use it, do I still need this part:

m_CurrentUser = HttpContext.Current.User.Identity.Name.ToString();

Thank you.
0
 

Author Comment

by:metropia
ID: 37744257
I have it like:

m_CurrentUser = HttpContext.Current.User.Identity.Name.ToString();
            m_CurrentUser = m_CurrentUser.split("\")(1)

but I get a message: "new line in constant"

I am using C# on VS 2010
0
 
LVL 38

Assisted Solution

by:PaulHews
PaulHews earned 350 total points
ID: 37744455
Try:
m_CurrentUser = m_CurrentUser.Substring(m_CurrentUser.LastIndexOf('\\') + 1);

Open in new window

0
 

Author Closing Comment

by:metropia
ID: 37744823
awesome! thanks for your help experts!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now