예제 #1
0
        /// <summary>
        /// Perform the authorization check using the Iprincipal on the action context.
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="resources"></param>
        /// <returns></returns>
        public static BoolMessage CheckAuthorization(IActionContext ctx, ILocalizedResourceManager resources)
        {
            // Principal object supplied.
            if (ctx.AuthenticationData != null && ctx.AuthenticationData.Identity.IsAuthenticated)
            {
                ctx.UserName = ctx.AuthenticationData.Identity.Name;

                // No roles required to perform this action.
                if (string.IsNullOrEmpty(ctx.AuthenticationRoles))
                {
                    return(BoolMessage.True);
                }

                bool   isAllowedToPerformAction = RoleUtils.IsInRoles(ctx.AuthenticationRoles, ctx.AuthenticationData);
                string error = isAllowedToPerformAction ? string.Empty : resources.GetString("Authorization_Failed",
                                                                                             "Authorizaion failed, not allowed to perform action per role.");

                return(new BoolMessage(isAllowedToPerformAction, error));
            }
            return(BoolMessage.False);
        }