Copied to clipboard

Flag this post as spam?

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


  • John Bergman 473 posts 1091 karma points
    1 week ago
    John Bergman
    0

    UmbracoIdentity - How do you programatically login a member?

    I've looked through the package's code, and cannot seem to piece together what is needed to do this.

    If I have the IMember object, how does one programmatically log the member in so that all downstream code and pages carry the authenticated member?

  • Huw Reddick 495 posts 1467 karma points
    1 week ago
    Huw Reddick
    0

    You use the MembershipHelper to log in a member, passing it the username and password of the member logging in.

    Members.Login(username, password)

  • John Bergman 473 posts 1091 karma points
    1 week ago
    John Bergman
    0

    I dont have the username and password.

  • Huw Reddick 495 posts 1467 karma points
    1 week ago
    Huw Reddick
    0

    So how do you know they are a valid member?

  • Huw Reddick 495 posts 1467 karma points
    1 week ago
    Huw Reddick
    0

    So how do you know they are a valid member?

  • John Bergman 473 posts 1091 karma points
    1 week ago
    John Bergman
    0

    External Authentication

  • Huw Reddick 495 posts 1467 karma points
    1 week ago
    Huw Reddick
    0

    You need to write a formsauthentication cookie, but you will still need a username to do that, so you will need to get that using your imember object

  • iNETZO 107 posts 433 karma points c-trib
    1 week ago
    iNETZO
    0

    Check https://github.com/Shazwazza/UmbracoIdentity/blob/e5af79578de92aa335ba5dbfb594ba9f6ad9173e/src/UmbracoIdentity.Web/App_Start/Controllers/UmbracoIdentityAccountController.cs#L74 , loginInfo contains the username.

    Depending on the type of external authentication, the username can be returned in loginInfo.DefaultUserName or in loginInfo.Login.ProviderKey

    We do this:

        string username = string.Empty;
        if (!string.IsNullOrEmpty(loginInfo.DefaultUserName))
        {
            username = loginInfo.DefaultUserName;
        } 
        else if (!string.IsNullOrEmpty(loginInfo.Login.ProviderKey))
        {
            username = loginInfo.Login.ProviderKey;
        }
    
        //find member by username
        var member = await UserManager.FindByNameAsync(username);
         if (member != null && Services.MemberService.GetById(member.Id).IsApproved)
         {
              await SignInAsync(member, isPersistent: false);
              return RedirectToLocal(returnUrl);
         }
    
Please Sign in or register to post replies

Write your reply to:

Draft