【问题标题】:Unauthorized Message,Status 200 OK未经授权的消息,状态 200 OK
【发布时间】:2018-04-30 06:53:32
【问题描述】:

我正在根据我最近开始工作的公司给出的给定架构做后端。 我是 C# 的新手,现在我正在尝试为一些 api-s 做一些 post/get/put 方法。 有一个我无法解决的问题。 邮递员 说: { “代码”:1, “消息”:“未授权” } 但是状态码是200 OK。

UserController.cs

[Route("v1/users")]
[Produces("application/json")]
public class UserController : BaseController
{
    /// <summary>
    ///     Get list of users (Authorize)
    /// </summary>
    /// <returns>
    /// </returns>
    [ProducesResponseType(typeof(BaseResponseModel<List<UserResource>>), 200)]
    [HttpGet]
    public async Task<IActionResult> Get()
    {
        var user = await _userService.GetUserResourcesAsync();

        return Success(user);
    }
 }

这没有任何意义,还是我很笨,没有意识到这个问题? 我有一个登录方法,我可以登录,我得到成功代码,然后我这样做: enter image description here Header

IProductService.cs

    public interface IProductService
{
    Task<ProductDto>  GetAsync(int id);
}

ProductService.cs

        public async Task<ProductDto> GetAsync(int id)
    {
        var product = await _context.Product.SingleOrDefaultAsync(p => p.Id == id);
        return _mapper.Map<ProductDto>(product);

    }

ProductDto.cs

 public class ProductDto
{
    public int Id { get; set; }

    public CategoryDto CategoryId { get; set; }

    public string Title { get; set; }

    public bool AllowEdit { get; set; }

    public string ItemCode { get; set; }

    public string CustomerCode { get; set; }
 }

Product.cs

[Table("Products")]
public class Product : DomainModel<int>
{
    [Required]
    public int ProductCategoryId { get; set; }

    [ForeignKey("ProductCategoryId")]
    public virtual ProductCategory ProductCategory { get; set; }

    [Required, StringLength(256)]
    public string Title { get; set; }

    [Required, DefaultValue(false)]
    public bool AllowEdit { get; set; }

    [StringLength(50)]
    public string ItemCode { get; set; }

    [StringLength(50)]
    public string CustomerCode { get; set; }
}

【问题讨论】:

  • 必须有一些标头或访问令牌什么的?另外,建议大家下载Postman最新的原生应用-getpostman.com/apps
  • 我有更新的问题可以看到编辑版本吗?标题链接?
  • 这条消息在正文中,正文对标头一无所知。它们的设置不同,并且没有相互连接。您可以在雕像为 404 时发送正确的数据。您可以根据您的情况使用 200 OK Statues 发送错误消息或错误数据
  • 你需要检查其他返回用户数据的函数的逻辑,并根据其他函数的输出设置这个web方法的状态
  • @SaIda - 代码按预期工作,正如您在 API 上定义的 ProducesResponseType。详情见我的回答。

标签: c# postman


【解决方案1】:

使用ProducesResponseTypeAttribute,属性API 实际上定义了指定类型的响应代码。见定义 在ProducesResponseTypeAttribute

工作原理

以下面的例子为例,如果对象为空,API 会抛出 404 错误:

public IActionResult GetById(string id)
{
    var post = <<Your logic here>>;
    if (post == null)
    {
        return NotFound();
    }

    return Json(post);
}

现在可以将相同的方法更改为以下方法,这将通过使用 ProducesResponseType 而返回 404,而不是在您的 API 逻辑中编写代码。

[ProducesResponseType((int)HttpStatusCode.NotFound)]
public IActionResult GetPostById(string id)
{
        var post = <<Your logic here>>;
        return Json(post);
    }

在某些情况下,为成功调用定义更明确的响应类型可能会更好。为此,请为带有类型的状态代码添加 ProducesResponseTypeAttribute。 (返回类型作为参数,这使得 Produces 的 Type 属性变得多余)。 这很有价值,如果你想从一个相同的方法返回不同的东西,例如,下面根据返回的状态码返回两种不同的类型:

[ProducesResponseType((int)HttpStatusCode.NotFound)]
[ProducesResponseType(typeof(Model), (int)HttpStatusCode.OK)]
public IActionResult GetById(string id)

你有什么问题

现在,如果您看到将这个属性定义为[ProducesResponseType(typeof(BaseResponseModel&lt;List&lt;UserResource&gt;&gt;), 200)] 的代码。以及获取用户的代码:

var user = await _userService.GetUserResourcesAsync();

返回BaseResponseModel&lt;T&gt;。 BaseResponseModel 应包含 CodeMessage 属性。所以这里API返回的响应是BaseResponseModel&lt;T&gt;的类型,所以API会返回属性定义的200 HTTP状态。

如何修复

您要么在出现Unauthorize 异常的情况下返回不同的对象,然后附加特定于该类型的ProducesResponseType,要么根据Authorize 属性处理未经授权的逻辑。

【讨论】:

  • 实际上这不是我想要的解决方案。修复是通过将授权类型更改为 OAuth 2.0。但现在我遇到了另一个问题。表示在激活特定注册期间发生的错误。它说在 .ProductService 类型上没有找到带有“Autofac.Core.Activators.Reflection.DefaultConstructorFinder”的构造函数。这个错误甚至意味着什么?
  • 这就是依赖注入相关的问题。
  • 是的,我尝试通过将其添加到 Startup.cs 来修复它: builder.RegisterType() .As();但现在会显示另一个问题:在激活特定注册期间发生错误。有关详细信息,请参阅内部异常。注册:Activator = ProductService (ReflectionActivator), Services = [..Service..IProductService] Lifetime = Autofac.Core.Lifetime.CurrentScopeLifetime, Sharing = Shared, Ownership = OwnedByLifetimeScope ---> 激活特定注册时出错.
  • 第一个错误是关于“Cannot resolve parameter .. of constructor ..”现在是“没有为该类型找到可访问的构造函数 ..”将其添加到启动文件后。 ¯_(ツ)_/¯
  • 能否请您发布有错误的代码。您提供的代码特定于ProducesResponseType,因此为此提供了答案。现在,您能否发布与依赖注入相关的场景,以便人们可以提出任何具体的更改建议。
猜你喜欢
  • 2010-10-16
  • 2020-08-12
  • 1970-01-01
  • 2016-06-22
  • 2015-09-14
  • 1970-01-01
  • 2018-02-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多