标签: asp.net-core-webapi

RS256 vs HS256:有什么区别?

我正在使用Auth0来处理我的网络应用程序中的身份验证.我正在使用ASP.NET Core v1.0.0和Angular 2 rc5,我对身份验证/安全性一般不太了解.

ASP.NET Core Web ApiAuth0文档中, JWT算法有两种选择:RS256和HS256.这可能是一个愚蠢的问题但是:

RS256和HS256有什么区别?有哪些用例(如果适用)?

jwt auth0 asp.net-core-webapi

216
推荐指数
4
解决办法
13万
查看次数

ASP.NET Core返回带有状态代码的JSON

我正在寻找在我的.NET Core Web API控制器中使用HTTP状态代码返回JSON的正确方法.我用它像这样:

public IHttpActionResult GetResourceData()
{
    return this.Content(HttpStatusCode.OK, new { response = "Hello"});
}
Run Code Online (Sandbox Code Playgroud)

这是一个4.6 MVC应用程序,但现在随着.NET核心我似乎没有这个IHttpActionResult我已经ActionResult和使用这样的:

public ActionResult IsAuthenticated()
{
    return Ok(Json("123"));
}
Run Code Online (Sandbox Code Playgroud)

但是来自服务器的响应很奇怪,如下图所示:

在此输入图像描述

我只是希望Web API控制器返回带有HTTP状态代码的JSON,就像我在Web API 2中所做的那样.

c# json asp.net-core asp.net-core-webapi

119
推荐指数
6
解决办法
20万
查看次数

如何从Controller返回特定的状态代码而没有内容?

我希望下面的示例控制器返回没有内容的状态代码418.设置状态代码很容易,但似乎需要做一些事情来发出请求结束的信号.在ASP.NET Core之前的MVC或WebForms中可能是一个调用,Response.End()但它如何在ASP.NET Core中Response.End不存在?

public class ExampleController : Controller
{
    [HttpGet][Route("/example/main")]
    public IActionResult Main()
    {
        this.HttpContext.Response.StatusCode = 418; // I'm a teapot
        // How to end the request?
        // I don't actually want to return a view but perhaps the next
        // line is required anyway?
        return View();   
    }
}
Run Code Online (Sandbox Code Playgroud)

c# asp.net-core-mvc asp.net-core asp.net-core-webapi

103
推荐指数
6
解决办法
11万
查看次数

在ASP.Net Core Web API中返回文件

问题

我想在我的ASP.Net Web API Controller中返回一个文件,但我的所有方法都返回HttpResponseMessage为JSON.

代码到目前为止

public async Task<HttpResponseMessage> DownloadAsync(string id)
{
    var response = new HttpResponseMessage(HttpStatusCode.OK);
    response.Content = new StreamContent({{__insert_stream_here__}});
    response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
    return response;
}
Run Code Online (Sandbox Code Playgroud)

当我在浏览器中调用此端点时,Web API会返回设置HttpResponseMessage为HTTP Content Header 的as JSON application/json.

c# .net-core asp.net-core asp.net-core-webapi

98
推荐指数
4
解决办法
9万
查看次数

Visual Studio 2019 不显示 .NET 6 Framework

我一直在努力将 ASP.NET Core Web API 项目的目标框架更改为 .NET 6,该项目是使用目标框架 .NET 5 启动的。我已经尝试了一些选项,但找不到列出的 .NET 6 Framework在目标下拉列表中。

  1. 我正在使用 Microsoft Visual Studio Enterprise 2019 版本 16.11.3。
  2. 我已经安装了.NET 6.0.0-rc.2.21480.5。这是通过 dotnet --version 验证的。
  3. 启用“使用 .NET Core SDK 预览”,并重新启动程序。
  4. 多次重新启动工作站,但仍然没有成功。

visual-studio asp.net-core asp.net-core-webapi .net-6.0

78
推荐指数
6
解决办法
16万
查看次数

从显式类型的ASP.NET Core API控制器(不是IActionResult)返回404

ASP.NET Core API控制器通常返回显式类型(默认情况下,如果您创建一个新项目),如下所示:

[Route("api/[controller]")]
public class ThingsController : Controller
{
    // GET api/things
    [HttpGet]
    public async Task<IEnumerable<Thing>> GetAsync()
    {
        //...
    }

    // GET api/things/5
    [HttpGet("{id}")]
    public async Task<Thing> GetAsync(int id)
    {
        Thing thingFromDB = await GetThingFromDBAsync();
        if(thingFromDB == null)
            return null; // This returns HTTP 204

        // Process thingFromDB, blah blah blah
        return thing;
    }

    // POST api/things
    [HttpPost]
    public void Post([FromBody]Thing thing)
    {
        //..
    }

    //... and so on...
}
Run Code Online (Sandbox Code Playgroud)

问题是return null;- 它返回HTTP 204:成功,没有内容.

然后,许多客户端Javascript组件将其视为成功,因此代码如下:

const …
Run Code Online (Sandbox Code Playgroud)

c# http-status-code-404 asp.net-core-mvc asp.net-core asp.net-core-webapi

50
推荐指数
4
解决办法
4万
查看次数

MediatR何时以及为何要使用它?vs 2017 webapi

它可能是之前被问过,但我甚至在官方网站上找不到为什么我应该使用MediatR以及它解决了什么问题?

  • 是因为我可以在构造函数中传递单个对象而不是多个接口?

  • 它是ServicesBus等的替代品还是竞争对手......

  • 基本上有什么好处,它解决了什么问题

我想购买它,但我不清楚为什么我应该使用它.

非常感谢

c# architectural-patterns asp.net-core-webapi

50
推荐指数
2
解决办法
2万
查看次数

如何将自定义标头添加到ASP.NET Core Web API响应

我正在将我的API从Web API 2移植到ASP.NET Core Web API.我曾经能够以下列方式添加自定义标头:

  HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
  response.Headers.Add("X-Total-Count", count.ToString());
  return ResponseMessage(response);
Run Code Online (Sandbox Code Playgroud)

如何在ASP.NET Core Web API中添加自定义标头?

c# asp.net-core-webapi

42
推荐指数
6
解决办法
5万
查看次数

ASP.NET Core 2.0 JWT验证因"用户授权失败:(null)"错误而失败

我正在使用ASP.NET Core 2.0应用程序(Web API)作为JWT发行者来生成移动应用程序的令牌消费品.不幸的是,这个令牌无法由一个控制器验证,而另一个控制器可以验证(在同一个asp.net核心2.0应用程序中使用相同的验证设置).

所以我有一个有效且可以解码的令牌,具有所有必需的声明和时间戳.但是一个端点接受它,而另一个端点给我401错误和调试输出:

Microsoft.AspNetCore.Authorization.DefaultAuthorizationService:信息:用户授权失败:(null).

[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[2]
      Authorization failed for user: (null).
Microsoft.AspNetCore.Authorization.DefaultAuthorizationService:Information: Authorization failed for user: (null).
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[3]
      Authorization failed for the request at filter 'Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter'.
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:Information: Authorization failed for the request at filter 'Microsoft.AspNetCore.Mvc.Authorization.AuthorizeFilter'.
Microsoft.AspNetCore.Mvc.ChallengeResult:Information: Executing ChallengeResult with authentication schemes ().
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Mvc.ChallengeResult[1]
      Executing ChallengeResult with authentication schemes ().
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler[12]
      AuthenticationScheme: Bearer was challenged.
Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler:Information: AuthenticationScheme: Bearer was challenged.
[40m[32minfo[39m[22m[49m: Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker[2]
      Executed action MyController.Get (WebApi) in 72.105ms
Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker:Information: Executed action MyController.Get (WebApi) in 72.105ms
Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: …
Run Code Online (Sandbox Code Playgroud)

c# bearer-token asp.net-core asp.net-core-webapi asp.net-core-identity

41
推荐指数
5
解决办法
2万
查看次数

从Asp.Net Core WebAPI返回jpeg图像

使用asp.net核心web api,我想让我的控制器动作方法返回一个jpeg图像流.
在我当前的实现中,浏览器仅显示json字符串.我的期望是在浏览器中看到图像.

在使用chrome开发人员工具进行调试时,我发现内容类型仍然存在

Content-Type:application/json; charset=utf-8

在响应头中返回,即使在我的代码中我手动将内容类型设置为"image/jpeg".

寻找解决方案 My Web API如下所示

[HttpGet]
public async Task<HttpResponseMessage> Get()
{
    var image = System.IO.File.OpenRead("C:\\test\random_image.jpeg");
    var stream = new MemoryStream();

    image.CopyTo(stream);
    stream.Position = 0;            
    result.Content = new StreamContent(image);
    result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
    result.Content.Headers.ContentDisposition.FileName = "random_image.jpeg";
    result.Content.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg");
    result.Content.Headers.ContentLength = stream.Length;

    return result;
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

content-type asp.net-core asp.net-core-webapi

38
推荐指数
2
解决办法
3万
查看次数