コード例 #1
0
        protected override Task HandleForbiddenAsync(AuthenticationProperties properties)
        {
            ForbiddenContext context = new ForbiddenContext(base.Context, base.Scheme, base.Options);

            base.Response.StatusCode = 403;
            return(Events.Forbidden(context));
        }
コード例 #2
0
        private Task OnMessageReceivedAsync(ForbiddenContext arg)
        {
            // For debugging purposes only!
            var s = $"OnMessageReceivedAsync: {arg.HttpContext}";

            arg.Response.ContentLength = s.Length;
            arg.Response.Body.WriteAsync(Encoding.UTF8.GetBytes(s), 0, s.Length);
            return(Task.FromResult(0));
        }
コード例 #3
0
        public override Task Forbidden(ForbiddenContext context)
        {
            context.Response.StatusCode  = 403;
            context.Response.ContentType = "application/json";
            var resp = new
            {
                ErrorCode = 403,
                message   = "Forbidden"
            };
            var options = new JsonSerializerSettings
            {
                ContractResolver = new CamelCasePropertyNamesContractResolver()
            };

            context.Response.WriteAsync(JsonConvert.SerializeObject(resp, options)).Wait();
            return(base.Forbidden(context));
        }
コード例 #4
0
        /// <summary>
        /// Returns an error message if authorization failed.
        /// </summary>
        /// <param name="arg"></param>
        /// <returns></returns>
        public static async Task AuthorizationFailed(ForbiddenContext arg)
        {
            // Check first if response was already handled
            if (!arg.Response.HasStarted)
            {
                var logger = arg.HttpContext.RequestServices.GetRequiredService <ILogger <JwtEventHelper> >();
                logger.LogInformation($"Authorization failed for user {arg.Principal}");

                var error = new MyProblemDetails(arg.HttpContext)
                {
                    Title     = "Authorization Error",
                    Detail    = "Missing access rights",
                    Status    = StatusCodes.Status403Forbidden,
                    Type      = "https://www.my-error-portal.com/myproject/403",
                    ErrorCode = "403"
                };

                // Add error message to response
                await WriteResponse(error, arg.Response, StatusCodes.Status403Forbidden);
            }
        }
コード例 #5
0
 /// <summary>
 /// Invoked if Authorization fails and results in a Forbidden response
 /// </summary>
 public virtual Task Forbidden(ForbiddenContext context) => OnForbidden(context);
コード例 #6
0
ファイル: Startup.cs プロジェクト: Kiwoon-Learning/Kiwoon_API
 public async Task OnAuthForbid(ForbiddenContext ctx)
 {
     ctx.Response.StatusCode = 200;
     await ctx.Response.WriteAsJsonAsync(new ApiResponse <string>(false, 403,
                                                                  "Token has expired due to a blacklist"));
 }
コード例 #7
0
 internal static Task OnForbidden(ForbiddenContext arg)
 {
     Debug.WriteLine("Access forbidden");
     return(Task.CompletedTask);
 }