Very Funny
Very Funny
CC#
Created by Very Funny on 11/16/2023 in #help
HttpClient is not being registered
No description
7 replies
CC#
Created by Very Funny on 11/16/2023 in #help
Unable to find endpoints. Invalid URI
I have this code on my controller
c#
[Route("api/[controller]")]
[ApiController]
[Authorize(Roles = "Admin")]
public class AdminController : ControllerBase
c#
[Route("api/[controller]")]
[ApiController]
[Authorize(Roles = "Admin")]
public class AdminController : ControllerBase
In other projects https://localhost:<PORT>/api/Admin/... would return the data. But now the URI is invalid. As it is a .NET 8 project, I am unsure as to how to add swagger as well
5 replies
CC#
Created by Very Funny on 11/15/2023 in #help
Dependency Injection not working (.NET 8)
No description
38 replies
CC#
Created by Very Funny on 11/15/2023 in #help
Unable to Promote or Demote users
I have buttons to promote and demote user roles, however they are currently doing nothing.
c#
@if (user.IsAdmin)
{
<button class="btn btn-danger" @onclick="() => DemoteUser(user.UserId)">Demote</button>
}
else
{
<button class="btn btn-success" @onclick="() => PromoteUser(user.UserId)">Promote</button>
}
...

@code
{
public List<UserInfo> Users { get; set; } = new ();

protected override async Task OnInitializedAsync()
{
var users = await UserManager.Users.ToListAsync();
foreach (var user in users)
{
var roles = await UserManager.GetRolesAsync(user);
var userInfo = new UserInfo
{
UserId = user.Id,
Email = user.Email,
Roles = new List<string>(roles),
IsAdmin = roles.Contains("Admin")
};
Users.Add(userInfo);
}
}

private async Task PromoteUser(string userId)
{
var user = await UserManager.FindByIdAsync(userId);
await UserManager.AddToRoleAsync(user, "Admin");
await OnInitializedAsync();
}

private async Task DemoteUser(string userId)
{
var user = await UserManager.FindByIdAsync(userId);
await UserManager.RemoveFromRoleAsync(user, "Admin");
await OnInitializedAsync();
}
}
c#
@if (user.IsAdmin)
{
<button class="btn btn-danger" @onclick="() => DemoteUser(user.UserId)">Demote</button>
}
else
{
<button class="btn btn-success" @onclick="() => PromoteUser(user.UserId)">Promote</button>
}
...

@code
{
public List<UserInfo> Users { get; set; } = new ();

protected override async Task OnInitializedAsync()
{
var users = await UserManager.Users.ToListAsync();
foreach (var user in users)
{
var roles = await UserManager.GetRolesAsync(user);
var userInfo = new UserInfo
{
UserId = user.Id,
Email = user.Email,
Roles = new List<string>(roles),
IsAdmin = roles.Contains("Admin")
};
Users.Add(userInfo);
}
}

private async Task PromoteUser(string userId)
{
var user = await UserManager.FindByIdAsync(userId);
await UserManager.AddToRoleAsync(user, "Admin");
await OnInitializedAsync();
}

private async Task DemoteUser(string userId)
{
var user = await UserManager.FindByIdAsync(userId);
await UserManager.RemoveFromRoleAsync(user, "Admin");
await OnInitializedAsync();
}
}
138 replies
CC#
Created by Very Funny on 11/12/2023 in #help
Authorised endpoint returns 401
I am trying to add authentication to my app. I followed a recent tutorial almost verbatim, however my code is not working. I have login and register endpoints that are working fine.
c#
//--------
AuthController.cs
...
private string CreateToken(User user)
{
List<Claim> claims = new()
{
new (ClaimTypes.Name, user.UserName),
new (ClaimTypes.Role, "Admin")
};

SymmetricSecurityKey key = new(Encoding.UTF8.GetBytes(_configuration["JwtSettings:Key"]!));

SigningCredentials creds = new(key, SecurityAlgorithms.HmacSha256Signature);

JwtSecurityToken token = new(
claims: claims,
expires: DateTime.Now.AddDays(30),
signingCredentials: creds
);

return new JwtSecurityTokenHandler().WriteToken(token);
}
//--------
Program.cs
...
services.AddAuthentication().AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
ValidateAudience = false,
ValidateIssuer = false,

IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(config["JwtSettings:Key"]!))
};
});
...
//-------
WeatherForecastController.cs
...
[HttpGet]
[Authorize(Roles = "Admin")]
public IEnumerable<WeatherForecast> Get() { ... }
c#
//--------
AuthController.cs
...
private string CreateToken(User user)
{
List<Claim> claims = new()
{
new (ClaimTypes.Name, user.UserName),
new (ClaimTypes.Role, "Admin")
};

SymmetricSecurityKey key = new(Encoding.UTF8.GetBytes(_configuration["JwtSettings:Key"]!));

SigningCredentials creds = new(key, SecurityAlgorithms.HmacSha256Signature);

JwtSecurityToken token = new(
claims: claims,
expires: DateTime.Now.AddDays(30),
signingCredentials: creds
);

return new JwtSecurityTokenHandler().WriteToken(token);
}
//--------
Program.cs
...
services.AddAuthentication().AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuerSigningKey = true,
ValidateAudience = false,
ValidateIssuer = false,

IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(config["JwtSettings:Key"]!))
};
});
...
//-------
WeatherForecastController.cs
...
[HttpGet]
[Authorize(Roles = "Admin")]
public IEnumerable<WeatherForecast> Get() { ... }
2 replies
CC#
Created by Very Funny on 11/12/2023 in #help
✅ Unable to use IdentityUser
I have installed Microsoft.AspNetCore.Identity into my project, but I am unable to access the class IdentityUser. Am I looking at the wrong package?
14 replies
CC#
Created by Very Funny on 10/22/2023 in #help
❔ Models files not appearing in Rider
No description
5 replies