c#
Is it possible to redirect request from middleware in .net core
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.AspNetCore.Antiforgery; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Identity; namespace Test.Middleware { public class TestMiddleware { private readonly RequestDelegate _next; public TestMiddleware(RequestDelegate next) { _next = next; } public async Task InvokeAsync(HttpContext httpContext, AppDbContext dataContext, UserManager<User> userManager, IAntiforgery antiforgery) { // Redirect to login … Read more
Is there any difference between an AutoResetEvent and a Semaphore with maximumCount = 1?
Yes, there certainly is a difference. A Semaphore is used to throttle access to a resource or block of code. When WaitOne is called a thread will block until a count from the semaphore becomes available. To make a count availabe you would call Release. A semaphore with a maximum count of 1 is often … Read more
Inject custom type conversion to .NET library classes
You can change the registered TypeConverter for something using TypeDescriptor.AddAttributes; this isn’t quite the same as Convert.ChangeType, but it may suffice: using System; using System.ComponentModel; static class Program { static void Main() { TypeDescriptor.AddAttributes(typeof(Guid), new TypeConverterAttribute( typeof(MyGuidConverter))); Guid guid = Guid.NewGuid(); TypeConverter conv = TypeDescriptor.GetConverter(guid); byte[] data = (byte[])conv.ConvertTo(guid, typeof(byte[])); Guid newGuid = (Guid)conv.ConvertFrom(data); } … Read more
Routes in ASP.net Core API
Try this. You can put a common route prefix on the controller. [Route(“api/[controller]”)] public class BXLogsController : Controller { //GET api/BXlogs/id/blah [HttpGet(“ID/{id}”, Name = “GetL”)] public IActionResult GetById(string id) { … } //GET api/BXlogs/api/blahapi [HttpGet(“API/{apiname}”, Name = “GetLAPI”)] public IActionResult GetByAPI(string apiname) { … } } read up on attribute routing here Routing to Controller … Read more
System.Text.Json.JsonException: The input does not contain any JSON tokens
Another reason this error could pop up, as it did for me, is simply because the API endpoint doesn’t exist because it was misspelled.
FileStream Vs System.IO.File.WriteAllText when writing to files [closed]
FileStream gives you a little more control over writing files, which can be beneficial in certain cases. It also allows you to keep the file handle open and continuously write data without relinquishing control. Some use cases for a stream: Multiple inputs Real time data from a memory/network stream. System.IO.File contains wrappers around file operations … Read more
ASP.NET Core – Add role claim to User
Well beside the answers, I just found the answer which is totally predefined in asp .net core. When you are adding claims just : var claims = new List<Claim> { new Claim(ClaimTypes.Name, UserName), new Claim(ClaimTypes.Role, “User”), new Claim(ClaimTypes.Role, “Admin”), new Claim(ClaimTypes.Role, Watever) }; after that you can just use it as said: [Authorize(Roles = “Watever”)] … Read more
How to properly read nested configuration values from config.json in ASP.NET5?
That’s the convention that we decided upon when we first created the configuration model. We started with json in mind and : is the delimiter there. Anyways, if you don’t want to worry about those conventions, I recommend using the ConfigurationBinder which binds a configuration to a model (a strong type object). Here are the … Read more
.NetCore JwtBearerAuthentication not rejecting expired tokens
I stumbled over the answer here if anyone is interested. Default value for ClockSkew is 5 minutes. app.UseJwtBearerAuthentication(new JwtBearerOptions() { AutomaticAuthenticate = true, AutomaticChallenge = true, TokenValidationParameters = new TokenValidationParameters() { ValidIssuer = Configuration[“Tokens:Issuer”], ValidAudience = Configuration[“Tokens:Audience”], ValidateIssuerSigningKey = true, IssuerSigningKey = new Certificate(certPath: Configuration[“Tokens:Certificate”], isValid: false).SecurityKey, ValidateLifetime = true, ValidateIssuer = true, ValidateAudience = … Read more