Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Jason 2 posts 23 karma points
    Jun 03, 2010 @ 07:27
    Jason
    1

    Programmatically login user with Member.AddMemberToCache()?

    I've got a registration page that automatically logs the user in when they register.
    At first I just had 1 line of code to log the user in:

    Member.AddMemberToCache(member);

    But after testing I noticed that this method causes the <asp:LoginName /> control to display the users UniqueID instead of their LoginName.
    My way around the problem was to add FormsAuthentication.SetAuthCookie() under the above. So now my code looks like:

    Member.AddMemberToCache(member);
    FormsAuthentication.SetAuthCookie(member.LoginName, false);

    Is this the correct way to log a user in programatically in umbraco? It feels a bit hacky to me.

    I'm using umbraco 4.0.4.1

    Thanks,
    Jason

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Jun 03, 2010 @ 10:52
    Lee Kelleher
    0

    Hi Jason,

    There is an overload method for "AddMemberToCache", that sets the AuthCookie too:

    AddMemberToCache(member, false, new TimeSpan(10, 0, 0, 0, 0))
    

    As far as I can tell, the TimeSpan isn't actually used in setting the expiration of the cookie - guess its a place-holder for future use?

    Cheers, Lee.

  • Jason 2 posts 23 karma points
    Jun 04, 2010 @ 03:21
    Jason
    0

    Thanks for the reply Lee, but that didn't fix my problem.

    The AuthCookie is being set by AddMemberToCache, but the username in the cookie (Page.User.Identity.Name) is being set to member.UniqueID instead of member.LoginName

    You can test it with the following:

    Member.AddMemberToCache(member, false, new TimeSpan(10, 0, 0, 0, 0));
    Response.Write(Page.User.Identity.Name);

     

    Cheers,
    Jason

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Jun 04, 2010 @ 09:33
    Lee Kelleher
    0

    Hi Jason,

    Sorry, just re-read your original post.  Yes, the workaround is the way you first suggested - setting the AuthCookie manually.

    If you think that functionality should be changed, you could raise a ticket on CodePlex?

    Cheers, Lee.

Please Sign in or register to post replies

Write your reply to:

Draft