Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi guys,does anyone have sample code that they have done that has multiple usernames declared in the web.config and backend c# code that uses it?

Regards
Posted

If I understand correctly, you are just defining few username-passwords in your Web.Config file as a key-value pair.
Later, you pick the username-password's key-value paid to authenticate. Now, your doubt is on how to proceed with multiple username-password in this flow.
If so,
Since, it's a custom thing, you can multiple logics to keep and retrieve it.
1. Keep a single key with all the username, password in it enclosed in brackets. Split it in code behind at the time of retrieval and then compare to see if anything matches.
HTML
<add key="username:password" value="{username1:password1},{username2:password2},{username3:password3},{username4:password4}">


2. Keep multiple username-passwords together in your web.config and then retrieve all of them at the time of authentication. In your code behind, get the values of the known keys and store it that can be used later for comparison.
XML
<add key="1:username:password" value="username1:password1">
<add key="2:username:password" value="username2:password2">
<add key="3:username:password" value="username3:password3">
<add key="4:username:password" value="username4:password4">



There can be various more ways, all you need is to stick with any one and just code to get the pairs in code behind. Use the pairs for comparison.
Keep it in an application variable or cache it to not retrieve again and again.
 
Share this answer
 
Comments
Vtec16 6-Jul-12 5:16am    
now this helped
Sandeep Mewara 6-Jul-12 5:30am    
Good to know.

Manas Bhardwaj 6-Jul-12 5:47am    
Good +5!
I didn't understood the question completely but I think These two article talks about this in details. you will also find sample projects to get a look at the code.

Understanding ASP.NET Roles and Membership - A Beginner's Tutorial[^]
Understanding and Implementing ASP.NET Custom Forms Authentication[^]

Let me know if these help in solving your issue.
 
Share this answer
 
v2
Comments
Vtec16 6-Jul-12 4:26am    
hey Rahul,that didnt help,i am not using any database or asp authentication controls..

i have them set up for a single user but theres a small change that allows you to create more than one user,..

currently in my web.config i have

<add key="username" value="username">
<add key="password" value="pa$$word">

and in my login page

protected void btnLogin_Click(object sender, EventArgs e)
{
string uname = txtUsername.Text;
string passwd = txtPassword.Text;


if (uname == userName && passwd == passWord)
{
//set
Session["username"] = uname;
//get
username = (string)Session["username"];




Response.Redirect("Single.aspx");


}
Rahul Rajat Singh 6-Jul-12 4:34am    
ok so you are using your custom logic for authentication. so i would still suggest that you check the second link titled "Understanding and Implementing ASP.NET Custom Forms Authentication". this is the best and standard way to do what you are trying to do.

following those practices will make your life very easy. otherwise you will end up having code for all the users and the code will contain very dirty conditional logic in a lot of places. Think about it. it might require some work now but it will save a lot of work later and perhaps a lot of rework and problems could also be avoided.

I hope I am not sounding counter-your-requirements. I am just suggesting the recommended way of doing this stuff.
Vtec16 6-Jul-12 5:15am    
thanks for trying mate
anyway....this is a method i used for the web config


<authentication mode="Forms">
 <forms loginUrl="~/SignIn.aspx" name=".ASPXAUTH" slidingExpiration="true" timeout="1440" path="/" defaultUrl="~/Default.aspx">
  <credentials passwordformat="Clear">
   <user name="testUser1" password="testPass1" />
   <user name="testUser2" password="testPass2" />
   <user name="testUser2" password="testPass3" />
  </credentials>
 </forms>
</authentication>



and code behind the login

<pre lang="c#">if (FormsAuthentication.Authenticate(this.txtUserName.Text, this.txtPassword.Text))
 {
  FormsAuthentication.SetAuthCookie(this.txtUserName.Text, false);
  FormsAuthentication.RedirectFromLoginPage(this.txtUserName.Text, false);
 }
 else
 {
  Response.Write("Invalid login details. Please try again.");
 }
 
Share this answer
 
v2
You Can do like this :-

XML
<authentication mode="Forms">
      <forms name="ContentAdmin" path="/" loginUrl="~/ContentAdmin/Login.aspx" defaultUrl="~/ContentAdmin/Default.aspx" protection="All" timeout="20"/>
    </authentication>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900