-
Notifications
You must be signed in to change notification settings - Fork 0
/
SecuredOperation.cs
40 lines (36 loc) · 1.09 KB
/
SecuredOperation.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using Core.Utilities.Interceptors;
using Core.Utilities.IoC;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Text;
using Castle.DynamicProxy;
using Microsoft.Extensions.DependencyInjection;
using Core.Extensions;
using Business.Constants;
namespace Business.BusinessAspects.Autofac
{
//JWT
public class SecuredOperation : MethodInterception
{
private string[] _roles;
private IHttpContextAccessor _httpContextAccessor;
public SecuredOperation(string roles)
{
_roles = roles.Split(',');
_httpContextAccessor = ServiceTool.ServiceProvider.GetService<IHttpContextAccessor>();
}
protected override void OnBefore(IInvocation invocation)
{
var roleClaims = _httpContextAccessor.HttpContext.User.ClaimRoles();
foreach (var role in _roles)
{
if (roleClaims.Contains(role))
{
return;
}
}
throw new Exception(Messages.AuthorizationDenied);
}
}
}