Похожие чаты

Is this the right way ? *i dont wanna use

too much abstractions with efcore and ms-identity as I am learning the framework adn c# from roots for this persoal project.
so i defined custom attributes
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class, Inherited = true, AllowMultiple = true)]
public class PermAttribute(params string[] allowedRoles) : Attribute, IActionFilter
{
private readonly string[] _allowedRoles = allowedRoles ?? throw new ArgumentNullException(nameof(allowedRoles));

public void OnActionExecuted(ActionExecutedContext context) { }

public void OnActionExecuting(ActionExecutingContext context)
{
IPermissionService permissionService = context.HttpContext.RequestServices.GetService<IPermissionService>() ?? throw new InvalidOperationException("Service error");
string currentRole = permissionService.CurrentRole();

bool isAuthorized = Array.Exists(_allowedRoles, role => role == currentRole);

//if (!isAuthorized) context.Result = new RedirectToActionResult("AccessDenied", "Error", null);
if (!isAuthorized)
{
// Assuming "AccessDenied" is the name of your view
ViewResult viewResult = new()
{
ViewName = "~/Views/Home/AccessDenied.cshtml"
};

context.Result = viewResult;
}
}
}
and used it in controllers like the authorize thingy but as action filters
[Perm("user", "admin", "editor")]
[HttpPost("/api/account/clearallsessions")]
public async Task<IActionResult> DisposeSessionKey()
{
if (await _authRepo.DisposeSessionKey(HttpContext.Session.GetString("username"))) return Ok();
else return BadRequest("unable to dispose session key");
}

1 ответов

42 просмотра

if i understand correctly. you use that method to check that user has access to that url or not. yes what you are doing is right.

Похожие вопросы

Обсуждают сегодня

Господа, а что сейчас вообще с рынком труда на делфи происходит? Какова ситуация?
Rꙮman Yankꙮvsky
29
А вообще, что может смущать в самой Julia - бы сказал, что нет единого стандартного подхода по многим моментам, поэтому многое выглядит как "хаки" и произвол. Короче говоря, с...
Viktor G.
2
@Benzenoid can you tell me the easiest, and safest way to bu.y HEX now?
Živa Žena
20
This is a question from my wife who make a fortune with memes 😂😂 About the Migration and Tokens: 1. How will the old tokens be migrated to the new $LGCYX network? What is th...
🍿 °anton°
2
30500 за редактор? )
Владимир
47
а через ESC-код ?
Alexey Kulakov
29
What is the Dex situation? Agora team started with the Pnetwork for their dex which helped them both with integration. It’s completed but as you can see from the Pnetwork ann...
Ben
1
Гайс, вопрос для разносторонее развитых: читаю стрим с юарта, нада выделять с него фреймы с определенной структурой, если ли чо готовое, или долбаться с ринг буффером? нада у...
Vitaly
9
Anyone knows where there are some instructions or discort about failed bridge transactions ?
Jochem
21
@lozuk how do I get my phex copies of my ehex from a atomic wallet, to move to my rabby?
Justfrontin 👀
11
Карта сайта