具有身份权限授权重定向循环的 Blazor 服务器
因此,我有一个新的 Blazor .net 6 应用程序,它使用 .net Identity 进行身份验证。我试图通过以下 this 来实现基于权限的授权教程。据我所知,我已经正确设置了所有内容,但是当我启动应用程序时,我立即遇到重定向循环、显示“页面未正确重定向”的错误以及大约 2600 个字符的 URL长的(https://localhost:7121/Identity/Account/Login?ReturnUrl=%252FIdentity%252FAccount%252FLogin%253FReturnUrl%253D%25252FIdentity%25252FAccount%25252FLogin%25253...)。
作为本教程的一部分,我向 Program.cs 添加了两项服务。
builder.Services.AddSingleton<IAuthorizationPolicyProvider, PermissionPolicyProvider>(); // Redirect error goes away if this is commented out
builder.Services.AddScoped<IAuthorizationHandler, PermissionAuthorizationHandler>();
如果我注释掉 PermissionPolicyProvider 行,重定向就会消失,我可以登录,但没有授权。如果我注释掉 PermissionPolicyProvider,登录,取消注释 PermissionPolicyProvider 并重新启动应用程序,授权就会起作用,但是一旦我注销,我就会立即收到重定向错误。当访问具有 [Authorize]
的页面和具有 [AllowAnonymous]
的页面时,会出现此错误。
这是 PermissionPolicyProvider:
public class PermissionPolicyProvider : IAuthorizationPolicyProvider
{
public DefaultAuthorizationPolicyProvider FallbackPolicyProvider { get; }
public PermissionPolicyProvider( IOptions<AuthorizationOptions> options )
{
FallbackPolicyProvider = new DefaultAuthorizationPolicyProvider( options );
}
public Task<AuthorizationPolicy> GetDefaultPolicyAsync() => FallbackPolicyProvider.GetDefaultPolicyAsync();
public Task<AuthorizationPolicy> GetPolicyAsync( string policyName )
{
if ( policyName.StartsWith( CustomClaimTypes.Permission, StringComparison.OrdinalIgnoreCase ) )
{
var policy = new AuthorizationPolicyBuilder();
policy.AddRequirements( new PermissionRequirement( policyName ) );
return Task.FromResult( policy.Build() );
}
return FallbackPolicyProvider.GetPolicyAsync( policyName );
}
public Task<AuthorizationPolicy> GetFallbackPolicyAsync() => FallbackPolicyProvider.GetDefaultPolicyAsync();
}
我的 App.razor 文件中有一个重定向,用于重定向未经身份验证的用户,该重定向工作正常,即使我从 App.razor 中删除重定向,我仍然收到重定向错误。此时我不太确定该去哪里寻找。当我实现 PermissionPolicyProvider 时,有什么我可能会忽略的事情吗?
So, I have a new Blazor .net 6 Application that is using .net Identity for authentication. I was attempting to implement permissions based authorization by following this tutorial. As far as I can tell I have everything set up correctly, but when I start the app I'm immediately greeted with a redirect loop, a an error that reads "The page isn't redirecting properly" and a URL that's about 2600 characters long (https://localhost:7121/Identity/Account/Login?ReturnUrl=%252FIdentity%252FAccount%252FLogin%253FReturnUrl%253D%25252FIdentity%25252FAccount%25252FLogin%25253...).
As part of the tutorial I added two services to my Program.cs.
builder.Services.AddSingleton<IAuthorizationPolicyProvider, PermissionPolicyProvider>(); // Redirect error goes away if this is commented out
builder.Services.AddScoped<IAuthorizationHandler, PermissionAuthorizationHandler>();
If I comment out the PermissionPolicyProvider line, the redirect goes away and I can log in but I have no authorization. If I comment out PermissionPolicyProvider, log in, uncomment PermissionPolicyProvider and restart the app, the authorization works, but as soon as I log out I immediately get the redirect error. The error shows up when accessing pages that have [Authorize]
and pages that have [AllowAnonymous]
.
Here is the PermissionPolicyProvider:
public class PermissionPolicyProvider : IAuthorizationPolicyProvider
{
public DefaultAuthorizationPolicyProvider FallbackPolicyProvider { get; }
public PermissionPolicyProvider( IOptions<AuthorizationOptions> options )
{
FallbackPolicyProvider = new DefaultAuthorizationPolicyProvider( options );
}
public Task<AuthorizationPolicy> GetDefaultPolicyAsync() => FallbackPolicyProvider.GetDefaultPolicyAsync();
public Task<AuthorizationPolicy> GetPolicyAsync( string policyName )
{
if ( policyName.StartsWith( CustomClaimTypes.Permission, StringComparison.OrdinalIgnoreCase ) )
{
var policy = new AuthorizationPolicyBuilder();
policy.AddRequirements( new PermissionRequirement( policyName ) );
return Task.FromResult( policy.Build() );
}
return FallbackPolicyProvider.GetPolicyAsync( policyName );
}
public Task<AuthorizationPolicy> GetFallbackPolicyAsync() => FallbackPolicyProvider.GetDefaultPolicyAsync();
}
There is a redirect in my App.razor file that redirects unauthenticated users, the redirect was working fine and I still get the redirect error even if I remove the redirect from App.razor. At this point I'm not really sure where to look. Is there something I may be overlooking when I implement the PermissionPolicyProvider?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将其更改
为:
解决了问题。
Changing this:
to this:
fixed the issue.