I'm creating custom Authorization in .net core. Everything is working completely fine but I want to add localizer in attribute response.
Below is my code
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
public class CustomAuthorizeFilters : AuthorizeAttribute, IAuthorizationFilter
{
public CustomAuthorizeFilters()
{ }
public void OnAuthorization(AuthorizationFilterContext context)
{
var User = context.HttpContext.User;
if (!User.Identity.IsAuthenticated)
return;
using (var account_help = new AccountHelpers(Startup.ConnectionString))
{
var userId = Guid.Parse(new security.AesAlgoridhm().Decrypt(User.Claims.Where(x => x.Type == JwtRegisteredClaimNames.Sid)?.FirstOrDefault().Value));
ProfileResponse user = new ProfileResponse();
if (user == null)
context.Result = new CustomResultFilters("error_access_token_expired", 401);
if (!user.EmailConfirmed)
context.Result = new CustomResultFilters("bad_response_account_not_confirmed", 400);
if (!user.Active)
context.Result = new CustomResultFilters("bad_response_account_inactive", 400);
}
}
}
I've tried passing localizer in the constructor like this but when I'm passing an argument from the controller it is giving me error as
attribute constructor parameter has type which is not a valid attribute parameter type
IStringLocalizer<dynamic> localize { get; set; }
public CustomAuthorizeFilters(IStringLocalizer<dynamic> localizer = null)
{
localize = localizer;
}
I know attribute only supports primitive datatypes that's why I've also tried injecting direct dependency as
context.HttpContext.RequestServices.GetService(typeof(IStringLocalizer<dynamic>));
but that is giving error as:
cannot convert from object to localizer.