将Swagger与ASP.NET Boilerplate一起使用会通过POST,PUT和Delete操作返回400错误请求。日志表明防伪令牌存在问题

时间:2018-09-12 14:50:33

标签: asp.net asp.net-core-mvc swagger swagger-ui aspnetboilerplate

我是ASP.NET Boilerplate的新手,我尝试使用一个简单的实体创建我的第一个应用程序服务。

这是实体:

public class Category : Entity
{
    public string Name { get; set; }
    public string ImageUrl { get; set; }
}

域名服务:

public class CategoryManager : DomainService, ICategoryManager
{
    private readonly IRepository<Category> _categoryRepository;

    public CategoryManager(IRepository<Category> categoryRepository)
    {
        _categoryRepository = categoryRepository;
    }
    public IEnumerable<Category> GetAll()
    {
        return _categoryRepository.GetAllList();
    }

    public Category GetById(int id)
    {
        return _categoryRepository.Get(id);
    }

    public Category Add(Category category)
    {
        return _categoryRepository.Insert(category);
    }


}

应用服务:

public class CategoryAppService : ApplicationService, ICategoryAppService
{
    private readonly ICategoryManager _categoryManager;

    public CategoryAppService(ICategoryManager categoryManager)
    {
        _categoryManager = categoryManager;
    }


    public ListResultDto<CategoryDto> GetAll(GetAllCategoriesInput input)
    {
        var categories = _categoryManager.GetAll();

        return new ListResultDto<CategoryDto>(ObjectMapper.Map<List<CategoryDto>>(categories));
    }

    public CategoryDto GetById(GetCategoryByIdInput input)
    {
        var category = _categoryManager.GetById(input.Id);
        return ObjectMapper.Map<CategoryDto>(category);
    }

    public CategoryDto Create([FromBody]CategoryDto input)
    {
        var category = _categoryManager.Add(ObjectMapper.Map<Category>(input));

        return ObjectMapper.Map<CategoryDto>(category);
    }


}

当我尝试使用swagger访问Get端点时,一切都按预期工作。但是,当我尝试访问其他端点时,它将返回400个未记录的错误。

Example of Invoking "Create" endpoint using Swagger

Example response

以下是我在输出窗口中找到的有关用摇摇欲坠的请求的一些信息:

    SampleService.Web.Mvc> info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
SampleService.Web.Mvc>       Request starting HTTP/1.1 POST http://localhost:62114/api/services/app/Category/Create application/json 86
SampleService.Web.Mvc> info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[1]
SampleService.Web.Mvc>       Route matched with {area = "app", action = "Create", controller = "Category"}. Executing action SampleService.Categories.AppServices.CategoryAppService.Create (SampleService.Application)
SampleService.Web.Mvc> info: Abp.AspNetCore.Mvc.Antiforgery.AbpAutoValidateAntiforgeryTokenAuthorizationFilter[1]
SampleService.Web.Mvc>       Antiforgery token validation failed. The required antiforgery header value "X-XSRF-TOKEN" is not present.
SampleService.Web.Mvc> Microsoft.AspNetCore.Antiforgery.AntiforgeryValidationException: The required antiforgery header value "X-XSRF-TOKEN" is not present.
SampleService.Web.Mvc>    at Microsoft.AspNetCore.Antiforgery.Internal.DefaultAntiforgery.ValidateRequestAsync(HttpContext httpContext)
SampleService.Web.Mvc>    at Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.ValidateAntiforgeryTokenAuthorizationFilter.OnAuthorizationAsync(AuthorizationFilterContext context)
SampleService.Web.Mvc> info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[3]
SampleService.Web.Mvc>       Authorization failed for the request at filter 'Abp.AspNetCore.Mvc.Antiforgery.AbpAutoValidateAntiforgeryTokenAuthorizationFilter'.
SampleService.Web.Mvc> info: Microsoft.AspNetCore.Mvc.StatusCodeResult[1]
SampleService.Web.Mvc>       Executing HttpStatusCodeResult, setting HTTP status code 400
SampleService.Web.Mvc> info: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
SampleService.Web.Mvc>       Executed action SampleService.Categories.AppServices.CategoryAppService.Create (SampleService.Application) in 0.6744ms
SampleService.Web.Mvc> info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
SampleService.Web.Mvc>       Request finished in 9.4144ms 400 
SampleService.Web.Mvc> info: Microsoft.AspNetCore.Server.Kestrel[32]
SampleService.Web.Mvc>       Connection id "0HLGPB44TBNED", Request id "0HLGPB44TBNED:00000001": the application completed without reading the entire request body.

请帮助。

谢谢。

0 个答案:

没有答案