Johnny
Johnny
Explore posts from servers
CC#
Created by Philémon on 8/20/2024 in #help
✅ PostgreSQL in docker container
At the end it is all on your local computer disk - you can imagine it as a "virtual machine" that runs linux (wsl) and on that "machine" are all you docker related files.
10 replies
CC#
Created by Johnny on 1/27/2023 in #help
❔ .net Identity 401 => Roles/Claims available
Just as information - seems like Swagger wasn't properly configured from me
36 replies
CC#
Created by Johnny on 1/27/2023 in #help
❔ .net Identity 401 => Roles/Claims available
Thanks for your time
36 replies
CC#
Created by Johnny on 1/27/2023 in #help
❔ .net Identity 401 => Roles/Claims available
Got it working
36 replies
CC#
Created by Johnny on 1/27/2023 in #help
❔ .net Identity 401 => Roles/Claims available
And I am not even authenticated
36 replies
CC#
Created by Johnny on 1/27/2023 in #help
❔ .net Identity 401 => Roles/Claims available
36 replies
CC#
Created by Johnny on 1/27/2023 in #help
❔ .net Identity 401 => Roles/Claims available
var builder = WebApplication.CreateBuilder(args);
ConfigurationManager configuration = builder.Configuration;

builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseNpgsql(configuration.GetConnectionString("ConnectionString")));

// For Identity
builder.Services.AddIdentity<IdentityUser, IdentityRole>().AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();

// Adding Authentication
builder.Services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
})

// Adding Jwt Bearer
.AddJwtBearer(options =>
{
options.SaveToken = true;
options.RequireHttpsMetadata = false;
options.TokenValidationParameters = new TokenValidationParameters()
{
ValidateIssuer = true,
ValidateAudience = true,
ValidAudience = configuration["JWT:ValidAudience"],
ValidIssuer = configuration["JWT:ValidIssuer"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration["JWT:Secret"]))
};
});
builder.Services.AddSingleton<IAuthorizationPolicyProvider, AuthorizationPolicyProvider>();
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(x =>
x.AddSecurityDefinition("token", new OpenApiSecurityScheme
{
Type = SecuritySchemeType.ApiKey,
In = ParameterLocation.Header,
Name = HeaderNames.Authorization,
Scheme = "Bearer"
})

);
var app = builder.Build();

if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();
var builder = WebApplication.CreateBuilder(args);
ConfigurationManager configuration = builder.Configuration;

builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseNpgsql(configuration.GetConnectionString("ConnectionString")));

// For Identity
builder.Services.AddIdentity<IdentityUser, IdentityRole>().AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();

// Adding Authentication
builder.Services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
})

// Adding Jwt Bearer
.AddJwtBearer(options =>
{
options.SaveToken = true;
options.RequireHttpsMetadata = false;
options.TokenValidationParameters = new TokenValidationParameters()
{
ValidateIssuer = true,
ValidateAudience = true,
ValidAudience = configuration["JWT:ValidAudience"],
ValidIssuer = configuration["JWT:ValidIssuer"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration["JWT:Secret"]))
};
});
builder.Services.AddSingleton<IAuthorizationPolicyProvider, AuthorizationPolicyProvider>();
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(x =>
x.AddSecurityDefinition("token", new OpenApiSecurityScheme
{
Type = SecuritySchemeType.ApiKey,
In = ParameterLocation.Header,
Name = HeaderNames.Authorization,
Scheme = "Bearer"
})

);
var app = builder.Build();

if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();
36 replies
CC#
Created by Johnny on 1/27/2023 in #help
❔ .net Identity 401 => Roles/Claims available
Thanks for your time
36 replies
CC#
Created by Johnny on 1/27/2023 in #help
❔ .net Identity 401 => Roles/Claims available
Is the pastebin link working?
36 replies
CC#
Created by Johnny on 1/27/2023 in #help
❔ .net Identity 401 => Roles/Claims available
36 replies
CC#
Created by Johnny on 1/27/2023 in #help
❔ .net Identity 401 => Roles/Claims available
36 replies
CC#
Created by Johnny on 1/27/2023 in #help
❔ .net Identity 401 => Roles/Claims available
Yea, I know that - but thanks anyway. It is good to remind someone of it
36 replies