Elsa Core 之 HttpEndpoint


提交数据的获取(liquid)

{{ Activities.NodeName.Input.Body }}
返回:
空

{{ Request.Path }}
返回:
workflows/commodityOrderAddEnd/handler

{{ Request.Body }}
返回:
空

{{ Activities.NodeName.Output|json }}
返回:
{"Path":"/workflows/commodityOrderAddEnd/handler","Method":"POST","QueryString":{},"Headers":{"Connection":"close","Content-Type":"application/json","Accept-Encoding":"gzip, deflate","Host":"119.91.152.190:25587","User-Agent":"vscode-restclient","Content-Length":"51"},"Body":{"name":"OrderPay.Confirm.End","args":1},"RawBody":"{\r\n  \"name\": \"OrderPay.Confirm.End\",\r\n  \"args\":1\r\n}"}



{{ Activities.NodeName.Output.Body }}
返回:
System.Dynamic.ExpandoObject


{{ Activities.NodeName.Output.RawBody }}
返回:
{
  "name": "OrderPay.Confirm.End",
  "args":1
}


{{ Activities.NodeName.Output.Body.name }}
返回:
OrderPay.Confirm.End

{{ Input }}
返回:
HttpRequestModel { Path = /workflows/commodityOrderAddEnd/handler, Method = POST, QueryString = System.Collections.Generic.Dictionary`2[System.String,System.String], Headers = System.Collections.Generic.Dictionary`2[System.String,System.String], Body = System.Dynamic.ExpandoObject, RawBody = {
  "id": 123,
} }

{{ Activities.Receiver.Output.Body.name }}

关于Security中Policy的使用

定义策略

// Add a custom policy.
services
    .AddAuthorization(auth => auth
        .AddPolicy("IsAdmin", policy => policy.RequireClaim("is-admin", "true")));

流程中要求验证策略

public class SecureHelloWorkflow : IWorkflow
{
    public void Build(IWorkflowBuilder builder)
    {
        builder
            .HttpEndpoint(setup => setup
                .WithPath("/safe-hello")
                .WithMethod("GET")
                .WithAuthorize()
                .WithPolicy("IsAdmin"))
            .WriteHttpResponse(setup => setup.WithStatusCode(HttpStatusCode.OK)
                .WithContent(context =>
                {
                    var httpContext = context.GetService<IHttpContextAccessor>().HttpContext!;
                    var user = httpContext.User;
                    return $"Hello {user.Identity!.Name}!";
                }));
    }
}

登录生成的token带claim为is-admin

public class TokenService : ITokenService
{
    private readonly JwtOptions _options;

    public TokenService(IOptions<JwtOptions> options)
    {
        _options = options.Value;
    }

    public string CreateToken(string userName, bool isAdmin)
    {
        var claims = new List<Claim>()
        {
            new(JwtRegisteredClaimNames.Sub, userName),
            new(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString())
        };

        if (isAdmin)
            claims.Add(new Claim("is-admin", "true"));

        var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_options.SecretKey));
        var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);
        var tokenDescriptor = new JwtSecurityToken(_options.Issuer, _options.Audience, claims, expires: DateTime.Now.AddYears(1), signingCredentials: credentials);
        return new JwtSecurityTokenHandler().WriteToken(tokenDescriptor);
    }

    public bool ValidateToken(string token)
    {
        var mySecret = Encoding.UTF8.GetBytes(_options.SecretKey);
        var mySecurityKey = new SymmetricSecurityKey(mySecret);

        var tokenHandler = new JwtSecurityTokenHandler();
        try
        {
            tokenHandler.ValidateToken(token, new TokenValidationParameters
            {
                ValidateIssuerSigningKey = true,
                ValidateIssuer = true,
                ValidateAudience = true,
                ValidIssuer = _options.Issuer,
                ValidAudience = _options.Audience,
                IssuerSigningKey = mySecurityKey,
            }, out SecurityToken validatedToken);
        }
        catch
        {
            return false;
        }

        return true;
    }
}


关键字: elsa