Log user in

A

Andrew Banks

Using forms authentication in C# I usually use
FormsAuthentication.RedirectFromLoginPage to log a user in as follows

FormsAuthentication.RedirectFromLoginPage(id,false);

How can I log a user in without re-directing them somewhere else? I simply
want to log the in and then run some other code without re-directing them.

I've tried .SetAuthCookie(id,false) but this doesn't seem to work.

Any ideas?
 
V

Viktor Jevdokimov

SetAuthCookie will set an Auth cookie for the next request, i.e. cookie will
be sent with the response. This way you can't use User.Identity to get an
information about just logged in user without receiving a new request with
the new Auth cookie just set - you need to use your own mechanism.
 
A

Andrew Banks

Sorry Viktor, I don't fully understand what you are saying.

Can anyone give an example of how I can log a user in without using
RedirectFromLoginPage?

Thanks
 
V

Viktor Jevdokimov

OK, I have a login form with the login button. In the LoginButton_Click
event handler method I validate user name and password in the DB. If
validation is successfult, I have this line of code for example:


System.Web.Security.FormsAuthentication.SetAuthCookie(strUserName, False,
"/")

"/" is the cookie path.
Below this line I have the code I need, and finally, a redirect, since my
controls and pagest looking for a Page.User.Identity, which will be changed
ONLY with the next request, since current request was without authentication
cookie you just set with "SetAuthCookie". What else?

What I'm saying, you have to know, how your application performs a request
step by step, and is it possible to use a request context to put an
authentication info (user name, etc) on the context, and check it somewhere
in your application, instead of Page.User.Identity...

Finally, how can you determine, that "SetAuthCookie" is not working?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top