Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 202
limos Avatar asked Sep 11 '25 23:09

limos


1 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.

like image 121
Daniel A. White Avatar answered Sep 14 '25 13:09

Daniel A. White



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!