C
C#2y ago
Johnny

❔ .net Identity 401 => Roles/Claims available

Hey, I keep getting 401 with the following bearer in Swagger (authorized)
Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoic3RyaW5nIiwianRpIjoiZDgxZTYzN2YtN2YyOS00YTJlLWFkMGQtMzUyYTNmMmM1MmNiIiwidXNlciI6WyJyZWFkIiwid3JpdGUiXSwiaHR0cDovL3NjaGVtYXMubWljcm9zb2Z0LmNvbS93cy8yMDA4LzA2L2lkZW50aXR5L2NsYWltcy9yb2xlIjpbIkFkbWluIiwiVXNlciJdLCJleHAiOjE2NzQ4MTc5OTYsImlzcyI6Imh0dHA6Ly9sb2NhbGhvc3Q6MjQ5NC8iLCJhdWQiOiJodHRwOi8vbG9jYWxob3N0OjI0OTQvIn0.3Z-DOjNZpucRtz0VbfJtAPZkKtFYkRrsJIpkeMcc6fI
Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoic3RyaW5nIiwianRpIjoiZDgxZTYzN2YtN2YyOS00YTJlLWFkMGQtMzUyYTNmMmM1MmNiIiwidXNlciI6WyJyZWFkIiwid3JpdGUiXSwiaHR0cDovL3NjaGVtYXMubWljcm9zb2Z0LmNvbS93cy8yMDA4LzA2L2lkZW50aXR5L2NsYWltcy9yb2xlIjpbIkFkbWluIiwiVXNlciJdLCJleHAiOjE2NzQ4MTc5OTYsImlzcyI6Imh0dHA6Ly9sb2NhbGhvc3Q6MjQ5NC8iLCJhdWQiOiJodHRwOi8vbG9jYWxob3N0OjI0OTQvIn0.3Z-DOjNZpucRtz0VbfJtAPZkKtFYkRrsJIpkeMcc6fI
I have created a controller for the role "Admin" and a controller for the policy "user.read" but both return me 401. Anyone an idea what the problem might be? I am using a custom policy "creator" that is creating a policy in the format "user.read" => user = ClaimType/read = ClaimValue
15 Replies
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Johnny
Johnny2y ago
Yea, I know that - but thanks anyway. It is good to remind someone of it
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Johnny
Johnny2y ago
Hastebin: Send and Save Text or Code Snippets for Free | Toptal®
Hastebin is a free web-based pastebin service for storing and sharing text and code snippets with anyone. Get started now.
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Johnny
Johnny2y ago
Pastebin
This site has been acquired by Toptal(Attention! API endpoint has c...
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
Johnny
Johnny2y ago
Is the pastebin link working?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Johnny
Johnny2y ago
Thanks for your time
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Johnny
Johnny2y ago
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();
Johnny
Johnny2y ago
I have no clue why but it seems that I am having no claims for some reason
Johnny
Johnny2y ago
And I am not even authenticated Got it working Thanks for your time Just as information - seems like Swagger wasn't properly configured from me
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server
More Posts
✅ dumb question involving Math.MaxGiven that `a` and `b` are 32-bit integers within the range of -100 to 100, this should always retur✅ Strange error when trying to retrun a class type variable through a public methodHi there, I am new to C# and while working on this method: ```namespace InventoryMaintenance { p❔ ✅ I can't convert List<BaseClass> to List<T> where T: BaseClass.I have a `List<List<Component>>`. `Component` is a base class, and each item in this outer list is a❔ Triggering an action after a complex on-screen dialog with a small time windowApologies for the confusing title but this is a *very* complex one to explain. I'm running a test t✅ Cant convert a list to list of a class type?I am new to C# and am stuck on this strange issue, I cant seem to send a list to a different class, ❔ How to preserve handwritten XML formattingIs there anyway to load a hand written XML file and add a few nodes and save it to the same file wit✅ Can't get this remove button working (entityframework, razorpages)Can someone tell me why my delete button isn't working?❔ ✅ Struggling with function showing error, "Object reference not set to an instance of an object"have been trying to solve this issue, not sure what the problem is. public static class CustomerDB ❔ Avalonia User ControlsHey! I'm creating a user control in Avalonia and would like some guidance on how to hook up a proper❔ CSV filesMy program runs fine, it creates accounts with a balance fine. However , after I try do a withdrawl