statto1974
statto1974
CC#
Created by Bulelani Botman on 2/20/2024 in #help
Trying To Use The UserManager.IsInRoleAsync() Function In My Controller
User.IsInRole("NORTH")
6 replies
CC#
Created by Bulelani Botman on 2/20/2024 in #help
Trying To Use The UserManager.IsInRoleAsync() Function In My Controller
You can use the IsInRole method of ClaimsPrincipal rather than doing a round trip to the database, but need to check that the user's roles are being added as claims
6 replies
CC#
Created by Bulelani Botman on 2/20/2024 in #help
Trying To Use The UserManager.IsInRoleAsync() Function In My Controller
IsInRoleAsync is expectinging an IdentityUser. The inherited User property from Controller is of type ClaimsPrincipal. You need to get the IdentityUser from _userManger first using one of the claims from the ClaimsPrincipal. var user = await _userManager.FindByIdAsync(id); Where id is a variable pulled out of one of the claims in the User property
6 replies
CC#
Created by Camster on 2/19/2024 in #help
MediatR in Class Library
Yeah basically inject it into the constructor
public class MyClass
{
private readonly IMediator _mediator;

public MyClass(IMediator mediator)
{
_mediator = mediator;
}

public async Task DoSomething()
{
await _mediator.Send(new DoSomethingCommmand());

await _mediator.Publish(new DoSomethingNotification());
}
}
public class MyClass
{
private readonly IMediator _mediator;

public MyClass(IMediator mediator)
{
_mediator = mediator;
}

public async Task DoSomething()
{
await _mediator.Send(new DoSomethingCommmand());

await _mediator.Publish(new DoSomethingNotification());
}
}
22 replies
CC#
Created by Camster on 2/19/2024 in #help
MediatR in Class Library
Also worth noting you would only need to use the MediatR.Contracts package in your class library and inject IMediator into your classes
22 replies