How to set/force a user account programmatically?
# help-with-umbraco
t
We have a repository for our Umbraco project for a client and an automated process to copy the production database to the local development environment. This allows for very fast environment setup for a new developer. However, the developer needs to log in as superadmin somehow. We are not allowed to add a shared generic superadmin account on the actual production setup. And we can't create prod user accounts for all developers either. Is the best way to run a direct SQL command to insert a generic "local development only" user or can you set it via appsettings somehow?
d
My company adds a little piece of code that is only compiled in debug mode. It skips the login and automatically makes you the admin user. Would that work in your case?
t
Absolutely! I suppose you could control it with an appsettings config as well if need be. How would you autologin as admin?
d
We have a middleware that checks if you're requesting something from the backoffice and if you are, we just take the backoffice user manager and call the login on that thing. Here's a snippet for you:
Copy code
csharp
public class AutoLoginMiddleware
{
    private readonly RequestDelegate _next;
    private readonly IRuntimeState _runtimeState;

    public AutoLoginMiddleware(RequestDelegate next, IRuntimeState runtimeState)
    {
        _next = next;
        _runtimeState = runtimeState;
    }

    public async Task InvokeAsync(HttpContext httpContext, IBackOfficeSignInManager signInManager, IBackOfficeUserManager backOfficeUserManager, IUmbracoContextAccessor umbracoContextAccessor)
    {
        // ignore this middleware as long as umbraco hasn't been initialised yet
        if (_runtimeState.Level < RuntimeLevel.Run)
        {
            await _next(httpContext);
            return;
        }

        // if PublishedRequest is null, request is not from frontend
        if (!IsAuthenticated(httpContext) && IsBackofficeRequest(umbracoContextAccessor) && RequestIsLocal(httpContext))
        {
            // login default user
            var user = await backOfficeUserManager.FindByIdAsync(Constants.Security.SuperUserIdAsString);
            await signInManager.SignInAsync(user, true);
        }

        await _next(httpContext);
    }

    private static bool IsBackofficeRequest(IUmbracoContextAccessor umbracoContextAccessor)
    {
        return umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext) && umbracoContext.PublishedRequest is null;
    }

    private static bool IsAuthenticated(HttpContext httpContext)
    {
        return httpContext.User.Identity?.IsAuthenticated ?? false;
    }

    private static bool RequestIsLocal(HttpContext httpContext)
    {
        var remoteAddress = httpContext.Connection.RemoteIpAddress?.ToString();

        // The requester may automatically sign in if they connect from a local ip or a local network ip
        return httpContext.Request.IsLocal()
            || (remoteAddress != null && remoteAddress.StartsWith("192.168", StringComparison.OrdinalIgnoreCase));
    }
}
t
Thank you! I'll have a look at and try this at work tomorrow.
11 Views