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

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

Get role/s of current logged in user in ASP.NET Core MVC

You may want to consider trying to load the actual ApplicationUser object via the FindByEmail() or some other method and passing that object into the GetRolesAsync() method as seen below : // Resolve the user via their email var user = await _userManager.FindByEmailAsync(model.Email); // Get the roles for the user var roles = await _userManager.GetRolesAsync(user); … Read more

ASP.NET Core Identity invalid token on confirmation email

This answer https://stackoverflow.com/a/31297879/2948212 pointed me in the right direction. But as I said it was for a different version and now it is slightly different solution. The answer is still the same: encode the token in base 64 url, and then decode it in base 64 url. That way both Angular and ASP.NET Core will … Read more

Generate access token with IdentityServer4 without password

[HttpPost(“loginas/{id}”)] [Authorize(Roles = “admin”)] public async Task<IActionResult> LoginAs(int id, [FromServices] ITokenService TS, [FromServices] IUserClaimsPrincipalFactory<ApplicationUser> principalFactory, [FromServices] IdentityServerOptions options) { var Request = new TokenCreationRequest(); var User = await userManager.FindByIdAsync(id.ToString()); var IdentityPricipal = await principalFactory.CreateAsync(User); var IdServerPrincipal = IdentityServerPrincipal.Create(User.Id.ToString(), User.UserName, IdentityPricipal.Claims.ToArray()); Request.Subject = IdServerPrincipal; Request.IncludeAllIdentityClaims = true; Request.ValidatedRequest = new ValidatedRequest(); Request.ValidatedRequest.Subject = Request.Subject; Request.ValidatedRequest.SetClient(Config.GetClients().First()); Request.Resources … Read more

NLog configuration in appsettings.json instead of nlog.config in .NET Core

Yes, this is possible but has a minimum version requirement. You must be using NLog.Extensions.Logging >= 1.5.0. Note that for ASP.NET Core applications this will be installed as a dependency if you install NLog.Web.AspNetCore >= 4.8.2. You can then create an NLog section in appsettings.json and load it with the following code: var config = … Read more

ASP.NET core it’s possible to configure an action in controller only in development mode?

This can be achieved by injecting IHostEnvironment into your controller and using its IsDevelopment() method inside of the action itself. Here’s a complete example that returns a 404 when running in anything other than the Development environment: public class SomeController : Controller { private readonly IHostEnvironment hostEnvironment; public SomeController(IHostEnvironment hostEnvironment) { this.hostEnvironment = hostEnvironment; } … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)