Solved

This property cannot be set for anonymous users - Problem assigning Profile properties when user should be authenticated. ASP.NET

Posted on 2010-09-09
6
1,206 Views
Last Modified: 2012-08-13
Hi Experts

I am trying to set a LastLoggedIn Profile Property at the point a user is authenticated via a Login Control. I am using the control's LoggedIn event to do this as below:

Protected Sub Login1_LoggedIn(ByVal sender As Object, ByVal e As System.EventArgs) Handles Login1.LoggedIn
            Profile.LastLoggedOn = DateTime.Now.ToString()
            Profile.Save()
End Sub

I had assumed that this event was raised once the user had been authenticated, but when I step into this User.Identity.IsAuthenticated is always False and so I get an error saying 'This property cannot be set for anonymous users'. If I remove this code however I am able to logon and pass authentication.

What am I doing wrong?

Many thanks

Stewart
0
Comment
Question by:digital_soul
[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
  • 3
  • 3
6 Comments
 
LVL 18

Expert Comment

by:carlnorrbom
ID: 33644648
Hi,

Have you put the Login control inside a LoginView control? If so that logic will certainly break, might break otherwise as well. Since You haven't posted any code related to the actual Login control I can't tell whether you have set the DestinationPageUrl property or not (i.e. DestinationPageUrl="~/Login.aspx") but I would expect you are posting back to the same page?!? The problem when using the LoginView control for instance is that as soon as the Login control posts back it will not be rendered inside the LoginView as it normally sits in the Anonymous template. What set the DestinationPageUrl property to the page itself and put the logic to update the profile object in the page load event instead such as:

#####Markup:

<form id="form1" runat="server">
<div>
    <asp:LoginView ID="LoginView1" runat="server">
        <AnonymousTemplate>
            <asp:Login ID="l1" runat="server" DestinationPageUrl="~/Login.aspx">
            </asp:Login>
        </AnonymousTemplate>
        <LoggedInTemplate>
            <asp:LoginStatus ID="ls1" runat="server" /><br />
            You are logged in as: <asp:LoginName id="ln1" runat="server" />
        </LoggedInTemplate>
    </asp:LoginView>
</div>
</form>

#####Code:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            If User.Identity.IsAuthenticated Then
                Profile.LastLoggedOn = DateTime.Now.ToLongDateString()
                Profile.Save()
            End If
        End If
    End Sub

The above works just fine for me.

/Carl.
0
 
LVL 2

Author Comment

by:digital_soul
ID: 33644725
Hi

This still doesn't work. If user browses to Index.aspx and is not authentiocated then they are automatically passed to login.aspx. From there I have the following control

<asp:Login ID="Login1" runat="server" DestinationPageUrl="~/Login.aspx">
</asp:Login>

and in the code behind

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            If User.Identity.IsAuthenticated Then
                Profile.LastLoggedOn = DateTime.Now.ToString()
                Profile.Save()
            End If
        End If
    End Sub

However, when I step into this User.Identity.IsAuthenticated always is still False, even though the user is then passed back to Insex.aspx authenticated and logged in.
0
 
LVL 18

Expert Comment

by:carlnorrbom
ID: 33645352
Hi,

Well, as i said in the previous post, if you post back to a different url you will never get the user authenticated during the postback to login.aspx. Put the logic in the Page_Load event of Index.aspx instead, there the user will be authenticated and You will get the same result!

/Carl.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Author Comment

by:digital_soul
ID: 33645399
But there is no guarantee that index.aspx will be the most appropriate URL to forward to. Depending on the user they might request one of any numbher of pages and, if not authenticated, be redirected to login.aspx. Ideally they should then be redirected to whatever page it was they originally requested. Does this mean I have to put this code into the onLoad event of every page? Also a full page refresh would then cause the profile property to be updated would it not? Are you saying there isn't a single event that I can tap into that occurs once the user is authenticated and update the profile properties at that point?
0
 
LVL 18

Accepted Solution

by:
carlnorrbom earned 500 total points
ID: 33654199
Hi,

Well, the problem is that you are redirecting while trying to tap into an event of a control of the page you are leaving. I would assume if you added a timeout to the redirection and had your own logic for this it would work. You will notice if you just for testing purposes take away the redirect and put a "If User.IsAuthenticated" clause in the page_load event of login.aspx that indeed when the page performs the full postback it will evaluate to true! So my suggestion would be to temporarily "halt" the redirection and display a message to the user for a couple of seconds or so and then proceed with the redirection to whatever url they should be directed to.

/Carl.
0
 
LVL 2

Author Closing Comment

by:digital_soul
ID: 33680290
Thanks. I ended up just overriding the ReturnUrl parameter and writing some redirect logic based on the authenticated users role.

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            If User.Identity.IsAuthenticated Then
                Profile.LastLoggedOn = DateTime.Now.ToString()
                Profile.Save()
                If User.IsInRole("Accounts") Then
                    Response.Redirect("~/WebForms/Accounts.aspx")
                Else
                    Response.Redirect("~/WebForms/General.aspx")
                End If
            End If
        End If
    End Sub

Protected Sub Login1_LoggedIn(ByVal sender As Object, ByVal e As System.EventArgs) Handles Login1.LoggedIn
        Response.Redirect(Login1.DestinationPageUrl)
End Sub
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

752 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