Faraz Shah Khan

MCP, MCAD.Net, MCSD.Net, MCTS-Win, MCTS-Web, MCPD-Web

Authentication using web.config Credentials

In one of my projects I have been asked to implement temporary security on sepcific module for internal usage and it was also suggested that I will not be putting more efforts on the same. First thing came in my mind was Membership/RoleProvider, but as I advised my planned solution they surprised me with the decision that I will not be using these providers and I should be using something more simpler and I was like Duh!!!

Any way I started thinking about a much more simpler solution as I have been alotted the biggest time period ever of 10-15 minutes max for this task :).

OK now I guess I should come to the point. During this process I realised that web.config credentials can be a good option as I personally don't wanted to implement any hardcoded authentication. It is very straight forward and simple but I had some problems implementing that solution so I thought to blog about how to authenticate using credentials incase if any one require the same.

All you need to do is to add some credentials in your web.config like this:

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

In the code block it can be authenticated like this:

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.");
 }

 

I hope it will be helpful for others as well. And yes it can be done in 15 minutes :)

Comments

Mukesh RAna said:

really a gud thing!! thnx a lot

# April 22, 2009 7:59 AM

Bradut Dima said:

Excellent,

Right when I needed...amazing that you have posted this only a couple of hours ago!

# April 23, 2009 12:40 AM

satalaj said:

This is really nice stuff. I will try now.

# June 4, 2009 1:17 AM

Jedi302 said:

Great post, just what i needed. it was so long ago i did a site with only one user so i forgot who to do..

thanks

# November 13, 2009 8:48 AM

&#26087;&#37329;&#23665;&#21326;&#20154; said:

"Hrmm that was weird, my remark acquired eaten. Anyhow I desired to say that it is awesome to learn that another individual furthermore described this as I had bother discovering the similar facts elsewhere. This was the first location that instructed me the answer. Thanks."

--------------------------------------------

<a href="xiangyan.info/8mg-p-119.html">&

Also welcome you!

# November 29, 2010 9:08 PM

crestron ipad app said:

Bad times make a good man.

-----------------------------------

# December 22, 2010 3:55 AM

ipad case said:

-----------------------------------------------------------

You really are a incredibly intelligent particular person!

# January 3, 2011 1:32 PM

dishwashers reviews said:

"How-do-you-do, just required you to find out We've additional your website to my Yahoo bookmarks because of your extraordinary blog site layout. But seriously, I assume  your site has one in the freshest theme I have came across. It actually aids make reading through your webpage a lot simpler."

--------------------------------------------------------------------    

Biology, Immunology

# January 17, 2011 12:13 PM

Bradly Yonkoske said:

Please let me know if you are searching for a writer for your blog. You've some truly wonderful articles and I believe I could be a good asset. For those who ever desire to take a few of the load off, I'd completely enjoy to write some content for your weblog in exchange for a link back to mine. Please shoot me an e-mail if interested.

# June 29, 2011 10:36 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)