asp.net-core-middleware

How to get User at Service Layer

放肆的年华 提交于 2019-11-28 07:34:37
问题 I use ASP.NET Core 2.1 and would like to fetch User at a service level . I've seen examples when HttpContextAccessor gets injected into some service and then we fetch the current User via UserManager var user = await _userManager.GetUserAsync(accessor.HttpContext.User); or in controller var user = await _userManager.GetUserAsync(User); Problems: Injecting HttpContextAccessor into service seems to be wrong - simply because we violate SRP and the Service Layer isn't isolated (it is dependant on

ASP.NET Core JWT Bearer Token Custom Validation

泪湿孤枕 提交于 2019-11-28 06:05:37
After a lot of reading, i have found a way to implement a custom JWT bearer token validator as below. Starup.cs Codes: public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime appLifetime) { loggerFactory.AddConsole(Configuration.GetSection("Logging")); loggerFactory.AddDebug(); app.UseStaticFiles(); app.UseIdentity(); ConfigureAuth(app); app.UseMvcWithDefaultRoute(); } private void ConfigureAuth(IApplicationBuilder app) { var signingKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes(Configuration.GetSection(

Redirect Non WWW to WWW using Asp.Net Core Middleware

此生再无相见时 提交于 2019-11-28 02:45:40
问题 On an ASP.Net Core application startup I have: RewriteOptions rewriteOptions = new RewriteOptions(); rewriteOptions.AddRedirectToHttps(); applicationBuilder.UseRewriter(rewriteOptions); When in Production I need to redirect all Non WWW to WWW Urls For example: domain.com/about > www.domain.com/about How can I do this using Rewrite Middleware? I think this can be done using AddRedirect and Regex: Github - ASP.NET Core Redirect Docs But not sure how to do it ... 回答1: A reusable alternative

asp.net core defaultProxy

白昼怎懂夜的黑 提交于 2019-11-27 11:45:08
问题 In net 4.5 we are working with proxy like this: <system.net> <!-- --> <defaultProxy enabled="true" useDefaultCredentials="false"> <proxy usesystemdefault="True" proxyaddress="http://192.168.1.1:8888" bypassonlocal="True" autoDetect="False" /> <module type="CommonLibrary.Proxy.MyProxy, CommonLibrary, Version=1.0.0.0, Culture=neutral" /> </defaultProxy> <settings> <httpWebRequest useUnsafeHeaderParsing="true" /> <servicePointManager expect100Continue="false" /> </settings> </system.net> but in

How do I customize ASP.Net Core model binding errors?

喜夏-厌秋 提交于 2019-11-27 02:43:16
问题 I would like to return only standardized error responses from my Web API (Asp.net Core 2.1), but I can't seem to figure out how to handle model binding errors. The project is just created from the "ASP.NET Core Web Application" > "API" template. I've got a simple action defined as: [Route("[controller]")] [ApiController] public class MyTestController : ControllerBase { [HttpGet("{id}")] public ActionResult<TestModel> Get(Guid id) { return new TestModel() { Greeting = "Hello World!" }; } }

ASP.NET Core JWT Bearer Token Custom Validation

僤鯓⒐⒋嵵緔 提交于 2019-11-27 01:11:39
问题 After a lot of reading, i have found a way to implement a custom JWT bearer token validator as below. Starup.cs Codes: public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime appLifetime) { loggerFactory.AddConsole(Configuration.GetSection("Logging")); loggerFactory.AddDebug(); app.UseStaticFiles(); app.UseIdentity(); ConfigureAuth(app); app.UseMvcWithDefaultRoute(); } private void ConfigureAuth(IApplicationBuilder app) { var

How to read ASP.NET Core Response.Body?

℡╲_俬逩灬. 提交于 2019-11-26 21:51:16
I've been struggling to get the Response.Body property from an ASP.NET Core action and the only solution I've been able to identify seems sub-optimal. The solution requires swapping out Response.Body with a MemoryStream while reading the stream into a string variable, then swapping it back before sending to the client. In the examples below, I'm trying to get the Response.Body value in a custom middleware class. Response.Body is a set only property in ASP.NET Core for some reason? Am I just missing something here, or is this an oversight/bug/design issue? Is there a better way to read Response

How to read ASP.NET Core Response.Body?

南笙酒味 提交于 2019-11-26 08:03:12
问题 I\'ve been struggling to get the Response.Body property from an ASP.NET Core action and the only solution I\'ve been able to identify seems sub-optimal. The solution requires swapping out Response.Body with a MemoryStream while reading the stream into a string variable, then swapping it back before sending to the client. In the examples below, I\'m trying to get the Response.Body value in a custom middleware class. Response.Body is a set only property in ASP.NET Core for some reason? Am I