我正在使用Auth0来处理我的网络应用程序中的身份验证.我正在使用ASP.NET Core v1.0.0和Angular 2 rc5,我对身份验证/安全性一般不太了解.
在ASP.NET Core Web Api的Auth0文档中, JWT算法有两种选择:RS256和HS256.这可能是一个愚蠢的问题但是:
RS256和HS256有什么区别?有哪些用例(如果适用)?
我正在寻找在我的.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中所做的那样.
我希望下面的示例控制器返回没有内容的状态代码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) 我想在我的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.
我一直在努力将 ASP.NET Core Web API 项目的目标框架更改为 .NET 6,该项目是使用目标框架 .NET 5 启动的。我已经尝试了一些选项,但找不到列出的 .NET 6 Framework在目标下拉列表中。
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
它可能是之前被问过,但我甚至在官方网站上找不到为什么我应该使用MediatR以及它解决了什么问题?
是因为我可以在构造函数中传递单个对象而不是多个接口?
它是ServicesBus等的替代品还是竞争对手......
基本上有什么好处,它解决了什么问题
我想购买它,但我不清楚为什么我应该使用它.
非常感谢
我正在将我的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中添加自定义标头?
我正在使用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
使用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)
asp.net-core ×7
c# ×7
.net-6.0 ×1
.net-core ×1
auth0 ×1
bearer-token ×1
content-type ×1
json ×1
jwt ×1