asp.net-core-middleware

Access TempData within custom middleware

你说的曾经没有我的故事 提交于 2019-12-12 20:24:23
问题 I have custom middleware that provides global error handling. If an exception is caught it should log the details with a reference number. I then want to redirect the user to an error page and only show the reference number. My research shows that TempData should be ideal for this but it only seems to be accessible from within a controller context. I tried adding the reference number to HttpContext.Items["ReferenceNumber"] = Guid.NewGuid(); But this value is lost through the redirect. How can

What exactly is 'UseAuthentication()' for?

試著忘記壹切 提交于 2019-12-12 07:39:16
问题 I have a question regarding authentication in ASP.NET Core 2: what exactly is the call app.UseAuthentication() for? Is it a basic prerequisite so that I can implement my custom authentication logic? I already had a look at the implementation of UseAuthentication and also of the actual middleware AuthenticationMiddleware, but to be honest, I don't understand what that is actually doing and why it would be necessary. To put it in another way: Do I need to call UseAuthentication() or is it a

ASP.NET Core Localization with help of SharedResources

老子叫甜甜 提交于 2019-12-12 07:08:37
问题 Hi I have a question about the SharedResources file. It is glanced over in the tutorial here: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/localization, and I'm not sure if I get it correctly. I am supposed to create a SharedResources.cs class, but where should i put it and should it be empty or do I need to fill it with some data? Same goes for the resource file, should I create a SharedResources.da.resx file and put all my shared strings there? Where should it go? And when I

User doesn't seem authenticated in the pipline inside`Use` in dotnetcore 2.0

人走茶凉 提交于 2019-12-10 17:08:53
问题 I am trying to provide an ActiveUser property to Serilog. Unfortunately I cannot seem to find the correct spot to check for the current user. In the below code httpContext.User.Identity.IsAuthenticated is always false? But only when logging in with the bearer token The bearer token login is working correctly insofar as the user is authenticated to the controller methods, and the user needs to belong to the correct roles in order to be authenticated. Though the user name is not correctly set -

How to write a middleware as custom router in asp.net core?

亡梦爱人 提交于 2019-12-08 04:00:27
问题 I want to transform this code: var trackPackageRouteHandler = new RouteHandler(context => { var routeValues = context.GetRouteData().Values; return context.Response.WriteAsync( $"Hello! Route values: {string.Join(", ", routeValues)}"); }); var routeBuilder = new RouteBuilder(app, trackPackageRouteHandler); routeBuilder.MapRoute( "Track Package Route", "package/{operation:regex(^track|create|detonate$)}/{id:int}"); routeBuilder.MapGet("hello/{name}", context => { var name = context

Access ModelState in Asp.net core Middleware

℡╲_俬逩灬. 提交于 2019-12-08 00:40:50
问题 I need to access ModelState in Asp.net Core 2.1 Middleware, but this is just accessible from Controller . For example I have ResponseFormatterMiddleware and in this Middleware I need to ignore ModelState error and show it's errors in 'Response Message': public class ResponseFormatterMiddleware { private readonly RequestDelegate _next; private readonly ILogger<ResponseFormatterMiddleware> _logger; public ResponseFormatterMiddleware(RequestDelegate next, ILoggerFactory loggerFactory) { _next =

How to write a middleware as custom router in asp.net core?

穿精又带淫゛_ 提交于 2019-12-07 20:37:31
I want to transform this code: var trackPackageRouteHandler = new RouteHandler(context => { var routeValues = context.GetRouteData().Values; return context.Response.WriteAsync( $"Hello! Route values: {string.Join(", ", routeValues)}"); }); var routeBuilder = new RouteBuilder(app, trackPackageRouteHandler); routeBuilder.MapRoute( "Track Package Route", "package/{operation:regex(^track|create|detonate$)}/{id:int}"); routeBuilder.MapGet("hello/{name}", context => { var name = context.GetRouteValue("name"); // This is the route handler when HTTP GET "hello/<anything>" matches // To match HTTP GET

IdentityServer4 client - Refreshing access tokens on CookieAuthenticationEvents

匆匆过客 提交于 2019-12-05 02:30:49
问题 I am trying to use refresh token when the access token expires. A similar so question is answered here. And a sample code to renew token by an action And i end up with the following code in the startup.cs app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationScheme = "Cookies", //ExpireTimeSpan = TimeSpan.FromSeconds(100), AutomaticAuthenticate = true, AutomaticChallenge = true, Events = new CookieAuthenticationEvents() { OnValidatePrincipal = async x => { if (x

How to remove server header using middleware?

徘徊边缘 提交于 2019-12-04 16:43:13
问题 In ASP.NET Core 1.0 every response will include the header Server: Kestrel . I want to remove this header along with other header like X-Power-By using middleware. I know that we can remove Kestrel header in host configuration by setting the following but I want to do it using middleware (actually when we have Httpmodule we can do like this so I am learning same thing). I tried my bit it did not work. new WebHostBuilder() .UseKestrel(c => c.AddServerHeader = false) Tried code: public class

ASP.NET Core 2.0 custom middleware using cookie authentication

守給你的承諾、 提交于 2019-12-03 13:51:03
问题 I need to implement custom "authentication" for my company. I say that in quotes because the user technically gets authenticated before it hits the application and if so, the userId will exist in request headers. What I need to do is figure out a way to query the database and get additional user information based on that Id, and set the HttpContext.User object so that it can be used easily within the application. The route I am taking now involves using Cookie Authentication without ASP.NET