Solved

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

Posted on 2012-03-20
8
300 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
Industry Leaders: 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 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

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 article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

726 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