我试图使网络应用程序与。net核心6和React,但有一个问题,当我试图使用API这是授权的特定角色。
这是我的准则
public class SessionBasedAuthorizationHandler : AuthorizationHandler<SessionBasedAuthorizationRequirement>
{
private readonly IHttpContextAccessor _httpContextAccessor;
public SessionBasedAuthorizationHandler(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, SessionBasedAuthorizationRequirement requirement)
{
string role = _httpContextAccessor.HttpContext.Session.GetString("Role");
if (role is null)
{
context.Fail();
return Task.CompletedTask;
}
if (role == requirement.Role.ToString())
{
context.Succeed(requirement);
return Task.CompletedTask;
}
else
{
context.Fail();
return Task.CompletedTask;
}
}
}
当我尝试使用具有特定角色的东西时,它在这一行中给我一个例外:
string role = _httpContextAccessor.HttpContext.Session.GetString("Role");
例外情况:
System.InvalidOperationException: 'Session has not been configured for this application or request.'
你能给我一个修理的建议吗?
1条答案
按热度按时间vq8itlhq1#
我在“program.cs”中解决了同样的问题,方法是
超过...的部分