?
Solved

ASP.Net 2.2 Custom Forms Authentication Not Working

Posted on 2008-06-13
6
Medium Priority
?
244 Views
Last Modified: 2012-05-05
I am trying to get some very basic user authentication setup with Forms Authentication and a custom database.

(test.aspx)
Users.AuthenticateUser("admin", "admin", true);
Response.Redirect("test2.aspx");

(test2.aspx)
Response.Write(Users.IsAuthed());
Response.Write(Users.GetUserDetails());

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

AuthenticateUser() checks the database with a SELECT statement looking for a username/password match, then sets the AuthCookie. I've tried this both ways below, but neither works.

      //first try
      FormsAuthentication.SetAuthCookie(ds.Tables[0].Rows[0]["user_id"].ToString(), remember_login);
      
      //second try
      FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(2, username, DateTime.Now, DateTime.Now.AddYears(10), true, "", FormsAuthentication.FormsCookiePath);

      HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket).ToString());
      cookie.HttpOnly = true;
      cookie.Path = FormsAuthentication.FormsCookiePath;
      cookie.Secure = FormsAuthentication.RequireSSL;
      cookie.Expires = ticket.Expiration;

      System.Web.HttpContext.Current.Response.Cookies.Clear();
      System.Web.HttpContext.Current.Response.Cookies.Add(cookie);

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

IsAuthed() is getting the User.Identity.IsAuthenticated property, but it always returns false.

GetUserDetails() is also returning -1, meaning User.Identity.Name is not returning a correct value either.

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

In my Web.Config

<forms
      name="SqlAuthCookie"
      path="/login"
      domain="http://www.storytalers.com"
      defaultUrl="~/admin/default.aspx"
      loginUrl="~/admin/login.aspx"
      timeout="120"
      cookieless="UseCookies" />
0
Comment
Question by:stephen_rushing
[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
6 Comments
 
LVL 33

Accepted Solution

by:
raterus earned 500 total points
ID: 21780214
Get rid of that path variable in web.config, unless EVERY page they could ever visit is under that directory.  Clear the cookies on the browser, and try again.
0
 

Author Comment

by:stephen_rushing
ID: 21780321
Thanks for the quick response!

The problem was the path and domain. I am developing locally, so neither should have been there. I added the path in the troubleshooting process, but just removing the domain would have saved me an hour or two and some frustration. Anyways, it all appears to be working perfectly now! Thanks again.
0
 

Author Closing Comment

by:stephen_rushing
ID: 31466973
Thanks for bringing my attention to the path attribute.
0
Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

 
LVL 6

Expert Comment

by:rafayali
ID: 21780340
Are you using ASPNETDB.MDF as database?

I see that you mention custom database, so you probably are not using ASPNETDB. Ignore the q above. So, how are you accessing this custom database?

That is, did you extend MembershipProvider Class?

Or, Did you just create a new class Users and wrote all logic in AuthenticateUser?

What is the code for AuthenticateUser?

Is the SQL in AuthenticateUser() returning correct resultset?

I think the best approach in this case would be to extend the MembershipProvider or SQLMembershipProvider.

Let me know the answers to the questions above.
0
 

Author Comment

by:stephen_rushing
ID: 21780356
In case that wasn't 110% clear for future questioners, here's the resulting forms node in my web.config.

<forms name="SqlAuthCookie" defaultUrl="~test2.aspx" loginUrl="~test.aspx" timeout="120" cookieless="UseCookies"/>
0
 

Author Comment

by:stephen_rushing
ID: 21780429
Thanks for your answer, rafayali. The "correct" database access was the path I was headed down in troubleshooting, but just didn't seem to be yielding anything.

I had confirmed AuthenticateUser was returning a result row from the database matching the username/pw.

Users is a custom class, not an extension. I've done very little OOP coding, so this project is a big learning experience. The finished product will be a Flex interface, so my ultimate goal is to interact with these classes via Flex. I'm not sure what benefit i might get by extending MembershipProvider.
0

Featured Post

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Introduction A frequently used term in Object-Oriented design is "SOLID" which is a mnemonic acronym that covers five principles of OO design.  These principles do not stand alone; there is interplay among them.  And they are not laws, merely princ…
A publishing tool, a Version Control System, or a Collaboration Platform! These can be some of the defining words for the two very famous web-hosting Git repositories: Bitbucket and Github. Git is widely used amongst the programmers and developers f…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…
Learn how to set-up custom confirmation messages to users who complete your Wufoo form. Include inputs from fields in your form, webpage redirects, and more with Wufoo’s confirmation options.

765 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