?
Solved

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

Posted on 2012-03-20
8
Medium Priority
?
304 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 38

Accepted Solution

by:
PaulHews earned 1400 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 600 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 38

Assisted Solution

by:PaulHews
PaulHews earned 1400 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
 

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 1400 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

765 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