473,413 Members | 1,989 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

Problem with FormsAuthentication

Hi all,

I am having a slight issue with FormsAuthentication.

I need to authenticate a user and while the page is still being processed,
need to work with that authenticated user. I have set up a test page as
follows...

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
Label1.Text = User.Identity.IsAuthenticated.ToString();
}

private void Button1_Click(object sender, System.EventArgs e)
{
FormsAuthentication.SetAuthCookie("David", false);
Label2.Text = User.Identity.Name;
Label3.Text = "Sign In Button Clicked";
}

private void Button2_Click(object sender, System.EventArgs e)
{
FormsAuthentication.SignOut();
Label2.Text = User.Identity.Name;
Label3.Text = "Sign Out Button Clicked";
}

When I click button1, I need label2.text to show "David", however, it does
not do this until I refresh the page (I can even click the sign out button,
then it will show "David" but only once.)

If I click button2, I expect it to sign me out, but as demonstrated, it
doesn't sign out straight away.

How else can I do this, without setting up a boolean property? I have done
some searching. The results suggest that when I SetAuthCookie or SignOut,
then I am logged in (or out, as the case may be).

Thanks.

Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
Nov 19 '05 #1
3 4871

David wrote:
private void Button1_Click(object sender, System.EventArgs e)
{
FormsAuthentication.SetAuthCookie("David", false);
Label2.Text = User.Identity.Name;
Label3.Text = "Sign In Button Clicked";
}

Why not:

private void Button1_Click(object sender, System.EventArgs e)
{
FormsAuthentication.SetAuthCookie("David", false);
Label2.Text = "David";
Label3.Text = "Sign In Button Clicked";
}

As in, you already have everything you need to know about the user when
you authorize him. Why would you want to look anything back up?

To answer your question though, take a look at the name of the method
you're calling: SetAuthCookie(). You are setting a cookie, which will
be sent along with the HTTPHeaders to the browser, and be returned to
you at the next request from that browser. At that point you'll be
able to read it. Before that, it's not part of the Cookies collection,
and thus not parsed by the ASP.NET helper functions that drop its value
back into User.Identity for you.

Hope this helps.
Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/

Nov 19 '05 #2
"jasonkester" <ja*********@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...

David wrote:
private void Button1_Click(object sender, System.EventArgs e)
{
FormsAuthentication.SetAuthCookie("David", false);
Label2.Text = User.Identity.Name;
Label3.Text = "Sign In Button Clicked";
}

Why not:

private void Button1_Click(object sender, System.EventArgs e)
{
FormsAuthentication.SetAuthCookie("David", false);
Label2.Text = "David";
Label3.Text = "Sign In Button Clicked";
}

As in, you already have everything you need to know about the user when
you authorize him. Why would you want to look anything back up?

To answer your question though, take a look at the name of the method
you're calling: SetAuthCookie(). You are setting a cookie, which will
be sent along with the HTTPHeaders to the browser, and be returned to
you at the next request from that browser. At that point you'll be
able to read it. Before that, it's not part of the Cookies collection,
and thus not parsed by the ASP.NET helper functions that drop its value
back into User.Identity for you.

Hope this helps.
Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/


Hi,

The example I posted was just a test to demonstrate what happens.

I know that SetAuthCookie sends a cookie down to the browser so that other
pages can read it later. What I was thinking was that it might also set a
flag somewhere in .NET whilst the page is running, showing me that the user
is Authenticated. Something like setting the Context properties, such as
User.Identity.IsAuthenticated = true and User.Identity.Name = "David".
Obviously not, so, I need to check the page later to see if the user is
Authenticated whilst the page is still running. How can I do this?

As an added information... the FormsAuthentication is done in a user
control. In the parent page, I am also checking if the
User.Identity.IsAuthenticated is set. As the two Identity values are only
gets, I need an alternative way to set them other than a round trip from the
browser.

Best regards,
Dave Colliver.
http://www.AshfieldFOCUS.com
~~
http://www.FOCUSPortals.com - Local franchises available
Nov 19 '05 #3
You could set a member variable on the user control when you
authenticate user. Throw in a public accessor to that variable and the
containing page will be able to read it.

Again, you'll have all the information you need at the time you call
SetAuthCookie. I agree it's sort of lame that you can't simply read
this information back out of User.Identity, but it's really not that
much extra work to stash it somewhere accessable for the remainder of
the page load.

My only idea as to why they designed it this way comes from looking at
their example code. In every case, the call immediately after
SetAuthCookie is RedirectFromLogin.

Good luck!

Jason Kester
Expat Software Consulting Services
http://www.expatsoftware.com/

Nov 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: francois | last post by:
hello, I am using forms authentication and I would like that my authentication cookie expires after let say 1 minutes (just for the exemple). When I log in in my longon page, the user has to...
4
by: Jeff B | last post by:
I am having a very perplexing problem with setting the user's roles. I have tried to figure this out for 2 days now. When the user logs in to the site, I retrieve the roles from the database and...
5
by: Archer | last post by:
I was making a role-based authentication but it does't login with correct password. the HttpContext.Current.User recieved in Global.asax is always null. Request.IsAuthenticated is always false....
3
by: JMUApache | last post by:
Hi: I have got a problem with FromsAuthentication for many days. I use "Forms" Authentication in my ASP.NET Web Froms, and I find that I can't singout.... Some Code Here: //In my...
3
by: Simon Harvey | last post by:
Hi All, I'm hoping somebody could help me with the following problem. I'm using forms authentication and the user is getting authenticated no problem. Once authenticated the user can look at...
0
by: Keithb | last post by:
I am using the login control on my web site, which has both authenticated an unauthenticated users. I added a logout page that is supposed to allow an authenticated user to drop his authentication...
1
by: =?Utf-8?B?SGFyZHkgV2FuZw==?= | last post by:
Hi all, I am running ASP.NET 2.0, after login I need to pass CustomerID in my database instead of username to other pages. I added following code to my login.aspx protected void...
1
by: jazzdrums | last post by:
Hello, we have migrated our website from .NET 1.1 to .NET 2.0. After this, some of our users are unable to log-on our site, while for the majority of them there's no problem. We're using a...
0
by: M# | last post by:
Hi everyone, I'm using WCF authentication services in my current project. I used the following information as a starting point: http://msdn.microsoft.com/en-us/library/bb398990.aspx ...
0
by: Rodrigo m. Ferreira | last post by:
Can you help me to solve the following problem? on my loggin page I have the code: protected void LoginButton_Click(object sender, EventArgs e) { if(Membership.ValidateUser(TXTUsuario.Text,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.