Code: Select all
Startup.csCode: Select all
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidIssuer = jwtSettings.Issuer,
ValidAudience = jwtSettings.Audience,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSettings.Key)),
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
};
});
app.UseMiddleware();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI(options =>
{
foreach (var description in provider.ApiVersionDescriptions)
{
options.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json", description.GroupName.ToUpperInvariant());
}
});
}
app.UseCors();
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
Code: Select all
string CreateToken()
{
var jwtSettings = configuration.GetSection(nameof(AppSettings.Jwt)).Get();
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(jwtSettings.Key));
var credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
var claims = new List
{
new Claim(JwtRegisteredClaimNames.Name, loginDto.Username)
};
var jwtSecurityToken = new JwtSecurityToken(
expires: DateTime.Now.AddMinutes(30),
claims: claims,
signingCredentials: credentials,
issuer: jwtSettings.Issuer,
audience: jwtSettings.Audience);
var jwt = new JwtSecurityTokenHandler().WriteToken(jwtSecurityToken);
return jwt;
}
Code: Select all
[ApiController]
[ApiVersion("1.0")]
[Route("api/[controller]")]
public class CustomerEnvironmentsController : ControllerBase
{
#region Fields
private readonly ICustomerEnvironmentsRepository customerEnvironmentsRepository;
private readonly IMapper mapper;
private readonly IDtoValidatorFactory apiValidatorFactory;
private readonly IHttpHeaderParser httpHeaderParser;
#endregion
#region Constructor
public CustomerEnvironmentsController(ICustomerEnvironmentsRepository customerEnvironmentsRepository, IMapper mapper, IDtoValidatorFactory apiValidatorFactory, IHttpHeaderParser httpHeaderParser)
{
this.customerEnvironmentsRepository = customerEnvironmentsRepository ?? throw new ArgumentNullException(nameof(customerEnvironmentsRepository));
this.mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
this.apiValidatorFactory = apiValidatorFactory ?? throw new ArgumentNullException(nameof(apiValidatorFactory));
this.httpHeaderParser = httpHeaderParser ?? throw new ArgumentNullException(nameof(httpHeaderParser));
}
#endregion
[Authorize]
[HttpGet]
public async Task GetCustomerEnvironments()
{
//Ommitted
}
}
Ich weiß einfach nicht, was ich sonst noch überprüfen soll.
Mobile version