.net core authorizationhandlercontext
C#
public class AccessToRouteHandler : AuthorizationHandler<AccessToRouteRequirement>
{
private readonly IHttpContextAccessor httpContextAccessor;
private readonly DbContext dbContext;
public AccessToRouteHandler(IHttpContextAccessor httpContextAccessor, DbContext dbContext)
{
this.httpContextAccessor = httpContextAccessor ?? throw new ArgumentNullException(nameof(httpContextAccessor));
this.dbContext = dbContext;
}
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, AccessToRouteRequirement requirement)
{
var filterContext = context.Resource as AuthorizationFilterContext;
var routeInfo = context.Resource as RouteEndpoint;
var response = filterContext?.HttpContext.Response;
if (!context.User.Identity.IsAuthenticated || string.IsNullOrEmpty(context.User.Identity.Name))
{
response?.OnStarting(async () =>
{
filterContext.HttpContext.Response.StatusCode = (int)System.Net.HttpStatusCode.Unauthorized;
});
context.Fail();
return Task.CompletedTask;
}
var verb = this.httpContextAccessor.HttpContext.Request.Method;
var routeKey = string.Empty;
if (context.Resource is Endpoint endpoint)
{
var cad = endpoint.Metadata.OfType<ControllerActionDescriptor>().FirstOrDefault();
var controllerFullName = cad.ControllerTypeInfo.FullName;
var actionName = cad.ActionName;
var bindings = cad.Parameters;
var actionParams = ".";
if (bindings.Any())
{
bindings.ToList().ForEach(p => actionParams += p.ParameterType.Name + ".");
}
routeKey = $"{controllerFullName}.{actionName}{actionParams}{verb}";
}
var route = dbContext.Routes
.Include(t => t.Roles)
.FirstOrDefault(r => r.RouteKey == routeKey);
if (route != null && route.Roles.Any(role => context.User.HasClaim(c => c.Value == role)))
{
// user belong to a role associated to the route.
context.Succeed(requirement);
return Task.CompletedTask;
}
response?.OnStarting(async () =>
{
filterContext.HttpContext.Response.StatusCode = (int)System.Net.HttpStatusCode.Forbidden;
});
context.Fail();
return Task.CompletedTask;
}
}
Also in C#:
- Title
- c# how to initialize an array
- Category
- C#
- Title
- no overload for 'useItemOnSceneLoad' matches delegate 'UnityAction<Scene, LoadSceneMode>'
- Category
- C#
- Title
- when do i need to end a sentence with ; in c#
- Category
- C#
- Title
- isInstanceOf nunit
- Category
- C#
- Title
- httpcontext in .net standard
- Category
- C#
- Title
- linq in c#
- Category
- C#
- Title
- order by C#
- Category
- C#
- Title
- uncaught syntaxerror invalid or unexpected token unity webgl
- Category
- C#
- Title
- add spaces between words unity
- Category
- C#
- Title
- c# string remove special characters
- Category
- C#
- Title
- array copy c#
- Category
- C#
- Title
- c# find element in list of list
- Category
- C#
- Title
- c# enum default
- Category
- C#
- Title
- list c#
- Category
- C#
- Title
- c# read all lines from filestream
- Category
- C#
- Title
- c# round number up
- Category
- C#
- Title
- degree between two points latitude longitude c#
- Category
- C#
- Title
- remove all array elements c#
- Category
- C#
- Title
- unity normalize vector2
- Category
- C#
- Title
- c# mixed multidimensional array
- Category
- C#
- Title
- unity rigidbody constraints
- Category
- C#
- Title
- background color with opacity
- Category
- C#
- Title
- how to create and trigger a function unity animation events
- Category
- C#
- Title
- c# string to float
- Category
- C#
- Title
- invalidoperationexception c# ui thread
- Category
- C#
- Title
- c# inotifypropertychanged best practices
- Category
- C#
- Title
- razor preview
- Category
- C#
- Title
- c# get time
- Category
- C#
- Title
- c# ref
- Category
- C#
- Title
- wpf use enum description
- Category
- C#
- Title
- add new page to site c# programmatically
- Category
- C#
- Title
- c# create new object
- Category
- C#
- Title
- c# remove first 5 characters from string
- Category
- C#
- Title
- asp.net core task iactionresult
- Category
- C#
- Title
- create expression func c# for use in where clause
- Category
- C#
- Title
- how to access first child of parent unity
- Category
- C#
- Title
- c# lambda expression
- Category
- C#
- Title
- C# enum
- Category
- C#
- Title
- F# convert generic.List to list
- Category
- C#
- Title
- uinput dialog uwp c#
- Category
- C#
- Title
- c# check to see if dictionary key exists
- Category
- C#
- Title
- c# convert dictionary to anonymous object
- Category
- C#
- Title
- C# Into To Tring Debug.Log
- Category
- C#
- Title
- compile in one single exe c#
- Category
- C#
- Title
- get web config key value in c# razor view
- Category
- C#
- Title
- c# read all text from a file
- Category
- C#
- Title
- how to create a variable in C#
- Category
- C#
- Title
- convert string to boolean c#
- Category
- C#
- Title
- if statement conditions c#
- Category
- C#
- Title
- 2d object look at object
- Category
- C#
- Title
- unity change text color
- Category
- C#
- Title
- unity c# run a command then wait
- Category
- C#
- Title
- snippet to create constructor in asp.net c#
- Category
- C#
- Title
- c# get index of item in list
- Category
- C#
- Title
- C# array index tostring
- Category
- C#
- Title
- c# windows grab screenshot
- Category
- C#
- Title
- native-googlesignin configuration is null!?
- Category
- C#
- Title
- how to textbox anywhere on chart in c#
- Category
- C#
- Title
- bulk update in c# using jquery datatble
- Category
- C#
- Title
- mailkit send attachment
- Category
- C#
- Title
- change z value unity
- Category
- C#
- Title
- convert string to decimal c#
- Category
- C#
- Title
- c# variable
- Category
- C#
- Title
- unity round vector 3 to nearest integer
- Category
- C#
- Title
- debug.log
- Category
- C#
- Title
- c# abstract class
- Category
- C#
- Title
- c# loop through repeater items
- Category
- C#
- Title
- get number of sundays in a month c#
- Category
- C#
- Title
- leantween id
- Category
- C#
- Title
- beard styles without mustache Intitle:work with me
- Category
- C#
- Title
- how to crouch in unity
- Category
- C#
- Title
- c# how to run external program
- Category
- C#
- Title
- c# sort for loop
- Category
- C#
- Title
- string to guid c#
- Category
- C#
- Title
- c# display image
- Category
- C#
- Title
- c# store generic type without arguments
- Category
- C#
- Title
- unity oncollisionenter2d
- Category
- C#
- Title
- query associative table ef6
- Category
- C#
- Title
- asp.net get query string parameter
- Category
- C#
- Title
- socket in c#
- Category
- C#
- Title
- access dic by key c#
- Category
- C#
- Title
- xamarin forms alarm
- Category
- C#
- Title
- unity how to get transform scale
- Category
- C#
- Title
- how to disable device guard or credential guard vmware
- Category
- C#
- Title
- creating a c# class
- Category
- C#
- Title
- json stringify c#
- Category
- C#
- Title
- c# get foreground window
- Category
- C#
- Title
- eager loading vs lazy loading c#
- Category
- C#
- Title
- c# get desktop path
- Category
- C#
- Title
- array to list C
- Category
- C#
- Title
- Read csv file into wpf C#
- Category
- C#
- Title
- c# dictionaries
- Category
- C#
- Title
- basic sorting algorithms c#
- Category
- C#
- Title
- generate a dropdown list from array data using razor .net mvc
- Category
- C#
- Title
- array syntax c#
- Category
- C#
- Title
- create dropdown in datatable c# dynamically
- Category
- C#
- Title
- while loop in c#
- Category
- C#
- Title
- using serial port in c#
- Category
- C#
- Title
- unity c# public all codes
- Category
- C#
- Title
- c# function return
- Category
- C#