Solved

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

Posted on 2012-03-20
8
299 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 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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
 

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

732 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