How to get the user's identity in Elsa Workflows?

Viewed 359

I set up Elsa Workflows Core in an ASP.NET Core app, with authentication for HTTP requests.

I have an HttpEndpoint activity, authentication is enforced, but I can't see any way to obtain the user's identity within the workflow.

The HttpContext object doesn't seem to be available. I can get the HttpRequestModel using GetInput(), but HttpRequestModel doesn't contain this information.

I'm using Microsoft AD authentication, but the question applies to any auth scheme.

2 Answers

If you are using dependency injection and running in a ASP.NET Core app, simply use constructor injection to get at IHttpContextAccessor.

class YourClass 
{
    public YourClass(IHttpContextAccessor httpContext)
    // ...
}

However this tightly couples your class with the HTTP context. You could create a view around it to return the things you need:

interface IUserAccessor
{
   User User { get; }
}
class HttpContextUserAccessor : IUserAccessor
{
   public YourClass(IHttpContextAccessor httpContext) //...
   public User User { get; } // implement using httpContext
}
class YourClass 
{
    public YourClass(IUserAccessor userAccessor)
    // ...
}

Also, it looks like Elsa has this pattern.

I solved it. The answer is here, in one of their samples.

Daniel put me on the right track, but my main problem was that I had .UseHttpActivities() before .UseAuthentication() in the Startup file.

Related