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 483 posts 1132 karma points
    Jun 09, 2021 @ 14:52
    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 1736 posts 6076 karma points MVP c-trib
    Jun 09, 2021 @ 19:59
    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 483 posts 1132 karma points
    Jun 09, 2021 @ 20:30
    John Bergman
    0

    I dont have the username and password.

  • Huw Reddick 1736 posts 6076 karma points MVP c-trib
    Jun 09, 2021 @ 21:02
    Huw Reddick
    1

    So how do you know they are a valid member?

  • Huw Reddick 1736 posts 6076 karma points MVP c-trib
    Jun 09, 2021 @ 21:02
    Huw Reddick
    0

    So how do you know they are a valid member?

  • John Bergman 483 posts 1132 karma points
    Jun 09, 2021 @ 22:06
    John Bergman
    0

    External Authentication

  • Huw Reddick 1736 posts 6076 karma points MVP c-trib
    Jun 10, 2021 @ 05:44
    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 133 posts 496 karma points c-trib
    Jun 10, 2021 @ 06:58
    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);
         }
    
  • John Bergman 483 posts 1132 karma points
    Jun 21, 2021 @ 17:36
    John Bergman
    0

    Hmm, that is what I was doing (but inline). Perhaps my logic is in the code that determines whether you are logged in. Does this look right?

        var loggedInUser = Context.GetOwinContext().Authentication.User;
    bool bIsAuthenticated = false;
    string userName = "";
    if (loggedInUser != null)
    {
        bIsAuthenticated = loggedInUser.Identity.IsAuthenticated;
        userName = loggedInUser.Identity.Name;
    }
    
    bool bIsLoggedin = Members.IsLoggedIn(); // Old way without OWIN/Identity extensions
    

    The bIsAuthenticated results in false, and the userName is not set either... additionally, the old way does also not realize that the user is logged in.

  • iNETZO 133 posts 496 karma points c-trib
    Jun 21, 2021 @ 20:03
    iNETZO
    1

    Try

    var loggedInUser = Request.GetOwinContext().Authentication.User;
    
Please Sign in or register to post replies

Write your reply to:

Draft