4

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.

limos
  • 1,526
  • 12
  • 13

2 Answers2

2

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.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • Thanks, I will try this. I was hoping Elsa had a way to give it to me natively. – limos Mar 09 '22 at 14:35
  • 1
    It didn't work. I did get a `HttpContext` object, but the User.Identity object didn't have any meaningful data in it (it existed, but everything was `null`.) It's the right context, it has the path and headers etc. From a regular controller or page it worked fine, so the auth is working - but it looks like it's been wiped by the time I need it. – limos Mar 09 '22 at 18:20
2

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.

limos
  • 1,526
  • 12
  • 13