Copied to clipboard

Flag this post as spam?

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


  • Mikael Mørup 297 posts 326 karma points
    May 03, 2010 @ 12:16
    Mikael Mørup
    0

    Logging user in programatically ?

    Is it possible to log a user (NOT a member) in programatically in umbraco 4.0.2.1 or 4.0.3 ?

  • Aaron Powell 1708 posts 3046 karma points c-trib
    May 03, 2010 @ 12:26
    Aaron Powell
    1

    Do you actually need to log them in (load any cookies, etc) or do you just need a reference to the User object that represents them?

    If it's the latter you could do new User(some id).

  • Tobias Neugebauer 52 posts 93 karma points
    May 03, 2010 @ 12:36
    Tobias Neugebauer
    1

    if you need to log the use in you need to take the BackofficeMembership provider and call the ValidateLogin-Method on it like this

    Membership.Providers[UmbracoSettings.DefaultBackofficeProvider].ValidateUser(USERNAME, PASSWORD)

    if that succeedes get the user and add the login to the database

    BusinessLogic.User u = new User(USERNAME);
    Guid userContextId = Guid.NewGuid();
    SqlHelper.ExecuteNonQuery(
                                          "insert into umbracoUserLogins (contextID, userID, timeout) values (@contextId,'" + u.Id + "','" +
                                          (DateTime.Now.Ticks + (_ticksPrMinute * _umbracoTimeOutInMinutes)).ToString() +
                                          "') ",
                                          SqlHelper.CreateParameter("@contextId", userContextId));

    And then add a cookie with the context

    HttpCookie cookie = new HttpCookie("UserContext");
    cookie.Name = "UserContext";
    cookie.Value = value;
    cookie.Expires = DateTime.Now.AddDays(1);
    HttpContext.Current.Response.Cookies.Add(cookie);

    The codes are taken from the login.aspx.cs and BasePage.cs you can take a look yourself

    Hope thats what you wanted :)

    Toby

     

  • Mikael Mørup 297 posts 326 karma points
    May 03, 2010 @ 13:28
    Mikael Mørup
    0

    I wish i could mark both suggestions as "solution".

    thank you both

    Mikael

     

Please Sign in or register to post replies

Write your reply to:

Draft