Solved

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

Posted on 2012-03-20
8
295 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

16 Experts available now in Live!

Get 1:1 Help Now