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?


This is a companion discussion topic for the original entry at https://our.umbraco.com/forum/106263-umbracoidentity-how-do-you-programatically-login-a-member

I’ve answered this here 'Admin' Member authentication - log in without password - #2 by jonathoncove3

This can be achieved with the MemberManager & MemberSigninManager

Quite simply, in v13:

using Umbraco.Cms.Core.Security;
using Umbraco.Cms.Web.Common.Security;

public class TokenAuthenticationService : ITokenAuthenticationService
{
    private readonly IMemberSignInManager _memberSignInManager;
    private readonly IMemberManager _memberManager;
    public static string CookieKey = "l_token";


    public TokenAuthenticationService(
        IMemberSignInManager memberSignInManager,
        IMemberManager memberManager)
    {
        _memberSignInManager = memberSignInManager;
        _memberManager = memberManager;
    }

    public async Task<bool> ProcessTokenAuthenticationAsync(HttpRequest request, HttpResponse response)
    {

        //get the memeber
        var memberIdentityUser = await _memberManager.FindByEmailAsync({ member_email});
        if (memberIdentityUser != null)
        {
            //sign in the member
            var signInTask = _memberSignInManager.SignInAsync(memberIdentityUser, true);
            await signInTask;

            if (signInTask.IsCompletedSuccessfully)
            {
                //horay!
            }
        }
    }
}

It can get a bit annoying using an async method, but other than that, it’s not too bad