Reputation: 319
I have to check in the code if the user has acess to the tool,i use this line to do this:
var appSetting = ConfigurationManager.AppSettings[APPSETTINGS.SUPPORTUSERS].ToUpper();
if ((loggedUser.Login.ToUpper() != appSetting))
The problem is in in the web.config file i have this:
<add key="SupportUsers" value="sis8ca"/>
I'm trying to put multiple values in this string,like "sis8ca , dts8ca , lgu8ca",if the person has acess,she can use it,but i cant pass multiple values like this,is there other way?
Upvotes: 1
Views: 2782
Reputation: 5712
Use something like this in your settings file:
<add key="SupportUsers" value="sis8ca,dts8ca,lgu8ca"/>
Then in your code behind do the following:
var appSetting = ConfigurationManager.AppSettings[APPSETTINGS.SUPPORTUSERS].ToUpper();
string[] supportUsers = appSetting.Split(',');
if (supportUsers.Contains(loggedUser.Login.ToUpper()))
{
// The rest of your code
}
Upvotes: 2
Reputation: 19081
Use:
<add key="SupportUsers" value="sis8ca;dts8ca;lgu8ca"/>
And the logic to check it:
var users = appSetting.Split(new[]{';'});
var hasAccess = users.Contains(loggedUser);
if (hasAccess){
...
}
Upvotes: 1
Reputation: 1250
Yes you can pass multiple values by comma(,)
separator and then when you call that key value use split
for eg.
string val = ConfigurationManager.AppSettings[APPSETTINGS.SUPPORTUSERS].ToUpper();
string[] words = val.Split(',');
you will get list of that words
Upvotes: 1
Reputation: 149518
The simplest solution would be to set a delimiter, and use string.Split
on that:
<add key="SupportUsers" value="sis8ca;dts8ca;lgu8ca"/>
var supportedUsers = ConfigurationManager.AppSettings["SupportUsers"]
.Split(';')
.ToList();
if (values.Contains(loggedUser.Login))
{
// Do stuff.
}
Upvotes: 2