Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
1,238 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
  • 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
Independent Software Vendors: 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Loops Section Overview
Suggested Courses

824 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