コード例 #1
0
        public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
        {
            var authorizedUser = context.HttpContext.User;

            if (!authorizedUser.Identity.IsAuthenticated)
            {
                await next();

                return;
            }

            var user = await GetAuthenticatedUser(authorizedUser);

            if (user == null)
            {
                context.Result = ResponseFormat.NotAuthMsg();
                return;
            }

            switch (CheckUserAccount(authorizedUser, user))
            {
            case 0:
                context.Result = ResponseFormat.PermissionDeniedMsg("حساب کاربری شما غیرفعال شده است.");
                return;

            case 2:
                context.Result = ResponseFormat.PermissionDeniedMsg("لطفا ابتدا حساب کاربری را تکمیل کنید.");
                break;

            case 3:
                context.Result =
                    ResponseFormat.PermissionDeniedMsg("حساب کاربری شما قبلا تکمیل شده است، لطفا مجددا وارد شوید.");
                break;
            }

            var routePolicy = GetRoutePolicy(context.ActionDescriptor.EndpointMetadata);

            if (CheckRoutePolicy(authorizedUser, routePolicy))
            {
                if (context.Result == null)
                {
                    ((ControllerExtension)context.Controller).AuthenticatedUser = user;
                    await next();
                }
            }
            else
            {
                context.Result = null;
                ((ControllerExtension)context.Controller).AuthenticatedUser = user;
                await next();
            }
        }
コード例 #2
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            // if (env.IsDevelopment()) {
            app.UseDeveloperExceptionPage();
            app.UseHangfireDashboard();
            // }
            // else {
            // app.UseHttpsRedirection();
            // }
            // if (env.IsProduction()) {
            //     app.UseExceptionHandler("/error");
            // }

            app.UseStatusCodePages(async context => {
                if (context.HttpContext.Response.StatusCode == 401 &&
                    context.HttpContext.Response.ContentType != "application/json")
                {
                    context.HttpContext.Response.ContentType = "application/json";
                    await context.HttpContext.Response.WriteAsync(
                        JsonSerializer.Serialize(ResponseFormat.NotAuth().Value));
                }
                else if (context.HttpContext.Response.StatusCode == 403 &&
                         context.HttpContext.Response.ContentType != "application/json")
                {
                    context.HttpContext.Response.ContentType = "application/json";
                    await context.HttpContext.Response.WriteAsync(
                        JsonSerializer.Serialize(ResponseFormat.PermissionDeniedMsg("شما به این قسمت دسترسی ندارید.")
                                                 .Value));
                }
                else if (context.HttpContext.Response.StatusCode == 400)
                {
                    context.HttpContext.Response.ContentType = "application/json";
                    await context.HttpContext.Response.WriteAsync(
                        JsonSerializer.Serialize(ResponseFormat.BadRequestMsg("درخواست نامعتبر").Value));
                }
                else if (context.HttpContext.Response.StatusCode == 500)
                {
                    context.HttpContext.Response.ContentType = "application/json";
                    await context.HttpContext.Response.WriteAsync(
                        JsonSerializer.Serialize(ResponseFormat.InternalError("مشکلی در سرور رخ داده است.").Value));
                }
            });

            app.UseSwagger();

            app.UseSwaggerUI(options => {
                options.SwaggerEndpoint("/swagger/V1 User/swagger.json", "V1 User");
                options.SwaggerEndpoint("/swagger/V1 Admin/swagger.json", "V1 Admin");
            });

            app.UseCors(x => x
                        .AllowAnyOrigin()
                        .AllowAnyMethod()
                        .AllowAnyHeader()
                        );

            app.UseRouting();

            app.UseAuthentication();

            app.UseAuthorization();

            app.UseStaticFiles();

            app.UseDirectoryBrowser();

            app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
        }