【发布时间】:2022-01-27 07:26:31
【问题描述】:
我在访问控制器时拥有此自定义授权,但我不断收到以下错误消息:谁能帮我识别代码中的问题?
//Controller
[GlobalDataAuthorization]
public ActionResult Index()
{
return View();
}
// Custom Authorization
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class GlobalDataAuthorization : AuthorizeAttribute
{
public async override void OnAuthorization(AuthorizationContext filterContext)
{
var authorizationBuilder = new AuthorizationBuilder();
bool hasAccess = await authorizationBuilder.GetUserAccess();
if (!hasAccess)
{
filterContext.Result = new RedirectResult("~/Home");
}
}
}
检查用户访问的生成器类:
public interface IAuthorizationBuilder
{
Task GetUserAccess();
}
public class AuthorizationBuilder : IAuthorizationBuilder
{
public async Task GetUserAccess()
{
var authConfigsResponse = await GetAuthConfigurationsAsync(); //http request
var allSupportedRoles = SetUpAllSupportedRoles(authConfigsResponse);
var supportedRoles = SetUpSupportedRoles(allSupportedRoles);
var result = await CheckClaimsAsync(supportedRoles, allSupportedRoles);//http request
return result;
}
}
-
async override void OnAuthorization
无效,您不能只采用同步方法并使其异步 - 调用者将无法等待它完成。