How do you create a custom AuthorizeAttribute in ASP.NET Core?

前端 未结 11 1444
春和景丽
春和景丽 2020-11-21 17:19

I\'m trying to make a custom authorization attribute in ASP.NET Core. In previous versions it was possible to override bool AuthorizeCore(HttpContextBase httpContext)

11条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-21 17:53

    For authorization in our app. We had to call a service based on the parameters passed in authorization attribute.

    For example, if we want to check if logged in doctor can view patient appointments we will pass "View_Appointment" to custom authorize attribute and check that right in DB service and based on results we will athorize. Here is the code for this scenario:

        public class PatientAuthorizeAttribute : TypeFilterAttribute
        {
        public PatientAuthorizeAttribute(params PatientAccessRights[] right) : base(typeof(AuthFilter)) //PatientAccessRights is an enum
        {
            Arguments = new object[] { right };
        }
    
        private class AuthFilter : IActionFilter
        {
            PatientAccessRights[] right;
    
            IAuthService authService;
    
            public AuthFilter(IAuthService authService, PatientAccessRights[] right)
            {
                this.right = right;
                this.authService = authService;
            }
    
            public void OnActionExecuted(ActionExecutedContext context)
            {
            }
    
            public void OnActionExecuting(ActionExecutingContext context)
            {
                var allparameters = context.ActionArguments.Values;
                if (allparameters.Count() == 1)
                {
                    var param = allparameters.First();
                    if (typeof(IPatientRequest).IsAssignableFrom(param.GetType()))
                    {
                        IPatientRequest patientRequestInfo = (IPatientRequest)param;
                        PatientAccessRequest userAccessRequest = new PatientAccessRequest();
                        userAccessRequest.Rights = right;
                        userAccessRequest.MemberID = patientRequestInfo.PatientID;
                        var result = authService.CheckUserPatientAccess(userAccessRequest).Result; //this calls DB service to check from DB
                        if (result.Status == ReturnType.Failure)
                        {
                            //TODO: return apirepsonse
                            context.Result = new StatusCodeResult((int)System.Net.HttpStatusCode.Forbidden);
                        }
                    }
                    else
                    {
                        throw new AppSystemException("PatientAuthorizeAttribute not supported");
                    }
                }
                else
                {
                    throw new AppSystemException("PatientAuthorizeAttribute not supported");
                }
            }
        }
    }
    

    And on API action we use it like this:

        [PatientAuthorize(PatientAccessRights.PATIENT_VIEW_APPOINTMENTS)] //this is enum, we can pass multiple
        [HttpPost]
        public SomeReturnType ViewAppointments()
        {
    
        }
    

提交回复
热议问题