help with asp.net/JS and cookies

Please support our ASP.NET advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
Reply

Join Date: Jun 2005
Posts: 77
Reputation: magusdf is an unknown quantity at this point 
Solved Threads: 0
magusdf's Avatar
magusdf magusdf is offline Offline
Junior Poster in Training

help with asp.net/JS and cookies

 
0
  #1
Jun 24th, 2005
hi,
i'm kind of new to asp
i used a few tutorials to put together a basic login page and to dump the information to a cookie.

overview:
i guess what i'm trying to accomplish is to have an ASP.net login page
and JavaScript validation though out the pages
it would be nice to check for some validation of the cookie to ensure its not just fabricated by user. but its not necessary at all
unfortunately time is an important factor so i try and skim corners where i can :evil:

Problem:
The cookie is created and the JS validation works
The problem is the cookie doesn’t terminate after x time I think it happens after reboot or a day
I’m not exactly how to approach this

And any help you can provide would be greatly appreciated.
:cheesy:
Thanks
MagusDF
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 175
Reputation: Letscode is an unknown quantity at this point 
Solved Threads: 6
Letscode's Avatar
Letscode Letscode is offline Offline
Junior Poster

Re: help with asp.net/JS and cookies

 
0
  #2
Jun 24th, 2005
Magus,
Can you provide these..
1.Your Authentication codes..Are you redirecting after the users have logged in.
2.I'm sure ,You must have made some changes in your webconfig file.Can you show us your authentication in your Webconfig file..





Originally Posted by magusdf
hi,
i'm kind of new to asp
i used a few tutorials to put together a basic login page and to dump the information to a cookie.

overview:
i guess what i'm trying to accomplish is to have an ASP.net login page
and JavaScript validation though out the pages
it would be nice to check for some validation of the cookie to ensure its not just fabricated by user. but its not necessary at all
unfortunately time is an important factor so i try and skim corners where i can :evil:

Problem:
The cookie is created and the JS validation works
The problem is the cookie doesn’t terminate after x time I think it happens after reboot or a day
I’m not exactly how to approach this

And any help you can provide would be greatly appreciated.
:cheesy:
Thanks
MagusDF
Save White Tiger
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 77
Reputation: magusdf is an unknown quantity at this point 
Solved Threads: 0
magusdf's Avatar
magusdf magusdf is offline Offline
Junior Poster in Training

Re: help with asp.net/JS and cookies

 
0
  #3
Jun 24th, 2005
yep
its actually a temporary solution until some one signs papers and i get acces to the lotus database
web config
  1. <configuration>
  2. <system.web>
  3. <authentication mode = "Forms">
  4. <forms name="CHeck" protection="All" timeout="10">
  5. <credentials passwordFormat = "Clear">
  6. <user name ="abc" password = "123"/>
  7. </credentials>
  8. </forms>
  9. </authentication>
  10. <authorization>
  11. <deny users = "?"/>
  12. </authorization>
  13. </system.web>
  14. </configuration>
asp/vb.net
  1. Sub doLogin(sender as object, e as eventargs)
  2. If FormsAuthentication.Authenticate(uname.text, pword.text) Then
  3. FormsAuthentication.SetAuthCookie(uname.text, true)
  4. Response.Redirect("ww_executive.htm")
  5. Else
  6. Response.Redirect("login.aspx")
  7. End If
  8. End Sub
javascript
  1. function run() {
  2. if (document.cookie.indexOf('CHeck')==-1){
  3. location.href="login.aspx";
  4. }
  5. else if (document.cookie.indexOf("CHeck")!=null) {
  6. location.href="ww_executive.htm";
  7. }
  8. }


i know this is probably atrocious. But my total exposure to asp is about 3 hours now.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 1,655
Reputation: tgreer is an unknown quantity at this point 
Solved Threads: 35
Team Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: help with asp.net/JS and cookies

 
0
  #4
Jun 24th, 2005
I don't understand the need for JavaScript here. ASP.NET Forms authentication handles it all for you, including managing the cookies.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 77
Reputation: magusdf is an unknown quantity at this point 
Solved Threads: 0
magusdf's Avatar
magusdf magusdf is offline Offline
Junior Poster in Training

Re: help with asp.net/JS and cookies

 
0
  #5
Jun 24th, 2005
well there is about 500 pages
i will be inserting java script tags to stop someone form skipping into a page unless they logged in

using just asp the way I have now
some one can just jump right in to a page by typing in the address
the java script redirects them back to the login page if the cookie doesn’t exist

It seems a lot easier for me to insert a java script for the validation rather than converting each of the pages to an asp page and redoing all the links.

My problem is that the cookie doesn’t disappear within the 10 minutes

I can take the java script element if I can insert a small asp script to do the validation but I really don’t know asp.net
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 1,655
Reputation: tgreer is an unknown quantity at this point 
Solved Threads: 35
Team Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: help with asp.net/JS and cookies

 
0
  #6
Jun 24th, 2005
You misunderstand ASP.NET Forms Authentication. It will handle all of this for you, automatically. You set it so that it authenticates the entire site, or particular directories. You don't have to write any JavaScript at all. If someone navigates to a page, ASP.NET will check to see if they are authenticated or not. If not, they are directed to your login page. Do a Google search of "ASP.NET Forms Authentication", and research it thoroughly.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 77
Reputation: magusdf is an unknown quantity at this point 
Solved Threads: 0
magusdf's Avatar
magusdf magusdf is offline Offline
Junior Poster in Training

Re: help with asp.net/JS and cookies

 
0
  #7
Jun 27th, 2005
thanks for the tip
i've read several pages on this
including
http://www.ondotnet.com/pub/a/dotnet...rmsauthp1.html
part 1 and 2

but i still seem to be missing somehting
if you can help point me towards the right direction

currently the pages themselves don't invoke the login
i haven't come across a overview or example that really goes in to it much

if you can point me in the right direction it would be greatly appreciated,

Thanks,
Magus
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 1,655
Reputation: tgreer is an unknown quantity at this point 
Solved Threads: 35
Team Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: help with asp.net/JS and cookies

 
0
  #8
Jun 27th, 2005
Please look at the following web.config file:

  1. <configuration>
  2. <system.web>
  3. <authentication mode="Forms">
  4. <forms name=".MYCOOKIE"
  5. loginUrl="login.aspx"
  6. protection="All"
  7. timeout="30"
  8. path="/"/>
  9. </authentication>
  10. <authorization>
  11. <deny users="?" />
  12. </authorization>
  13. </system.web>
  14. </configuration>

The properties/attributes of the "forms" element control your authentication cookie. The "name" specifies the name of the cookie. "protection" is how your cookie will be protected. "All" means it will be validated and encrypted. "Timeout" controls when your cookie will expire, in minutes.

Notice also the "loginurl" property. It points to "login.aspx" in this sample. This is the page YOU MUST WRITE.

In the "authorization" section, you can see we're denying access to all anonymous users. This means no matter which page they link to, if they're not authenticated, they'll be redirected to the login.aspx.

What should your login.aspx page do? Prompt for username and password, obviously. You can compare the values the user enters to something you've hardcoded, or you can look the values up in the database, whatever you like!

Once you've determined, through whatever mechanism you care to code, that the username and password are valid, you have to let the authentication mechanism know they are a valid, authenticated user.

You do this by using the FormsAuthentication class in the code-behind of your login.aspx page.

There are a number of methods & properties, including .Authenticate, and .RedirectFromLogin.

You can also store your usernames and passwords within the web.config file itself, by adding a "credentials" node:

  1. <credentials passwordFormat="Clear">
  2. <user name="user1" password="password1"/>
  3. <user name="user2" password="password2"/>
  4. <user name="user3" password="password3"/>
  5. </credentials>

This is perhaps the easiest way. Then in your login.aspx page, you can use the Authenticate method. Say you have a field named "username" and one named "password", and a button named "login":

  1. void Login_Click(Object sender, EventArgs E)
  2. {
  3. // authenticate user: this sample authenticates
  4. // against users in your app domain's web.config file
  5. if (FormsAuthentication.Authenticate(username.Value,
  6. password.Value))
  7. {
  8. FormsAuthentication.RedirectFromLoginPage(username.Value, true);
  9. //the boolean persists the cookie.
  10. }
  11. else
  12. {
  13. lblResults.Text = "Invalid Credentials: Please try again";
  14. }
  15. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 77
Reputation: magusdf is an unknown quantity at this point 
Solved Threads: 0
magusdf's Avatar
magusdf magusdf is offline Offline
Junior Poster in Training

Re: help with asp.net/JS and cookies

 
0
  #9
Jun 27th, 2005
apologies for being so persistent
but i already have both of those set up and login.aspx works

i guess the part that i'm confused about is the individual pages?
should there be some script or something that checks the verification
or is it skipping because its a configuration on the server end

unfortunately i don't have access to the server and there is about 5 pages of paper that need to be pushed each time i want to change a setting


but if some one types in
EX:
url/pagename.htm
www.somedomain/sales.htm

it will skip the validation presses

that is the part i'm lost at
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 1,655
Reputation: tgreer is an unknown quantity at this point 
Solved Threads: 35
Team Colleague
tgreer tgreer is offline Offline
Made Her Cry

Re: help with asp.net/JS and cookies

 
0
  #10
Jun 27th, 2005
If "sales.htm" is in the same folder as the web.config file, authentication will be automatic. If a user tries to browse that specific page, ASP.NET first checks the web.config file to see if anonymous browsing is allowed. If it isn't, it looks for instructions on how to authenticate. It finds them in the <forms> node, and redirects the user to login.aspx. The login.aspx page authenticates the user, and then redirects them to their original page, in this case, sales.htm.

Nothing special needs to go on the sales.htm page.

If you've set all this up and it isn't working, then you haven't set it all up correctly!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the ASP.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC