C# 执行授权签入代码隐藏

C# 执行授权签入代码隐藏,c#,asp.net-core,blazor,C#,Asp.net Core,Blazor,在blazor页面中,如果用户有特定的策略,我想(显示/隐藏/设置为只读/更改样式…等等)一个文本框 因此,为了实现(显示和隐藏),我做了以下工作: <AuthorizeView Policy="CanReadNamePolicy"> <Authorized Context="test"> <inputText @Bind-Value="@Name"/> </Authorized> </Authoriz

在blazor页面中,如果用户有特定的策略,我想(显示/隐藏/设置为只读/更改样式…等等)一个文本框 因此,为了实现(显示和隐藏),我做了以下工作:

 <AuthorizeView Policy="CanReadNamePolicy">
     <Authorized Context="test">
        <inputText @Bind-Value="@Name"/>
     </Authorized>
 </AuthorizeView>

如果可能的话,您是否知道如何在后台执行授权签入代码

以下是一段代码片段:

如果程序逻辑要求应用程序检查授权规则,请使用Task类型的级联参数获取用户的ClaimsPrincipal。任务可以与其他服务(如IAuthorizationService)组合以评估策略

@InjectIAuthorizationService授权服务
做一些重要的事情
@代码{
[CascadingParameter]
私有任务authenticationStateTask{get;set;}
私有异步任务DoSomething()
{
var user=(等待authenticationStateTask);
if((等待AuthorizationService.authorizationAsync(用户,“CanReadNamePolicy”))
(成功)
{
//执行仅对满足以下条件的用户可用的操作:
//“CanReadNamePolicy”策略。
}
}
}
注:

  • InputText组件必须位于EditForm组件中
  • 它是
    @bind Value
    而不是
    @bind Value
  • 一个策略可以评估多个需求。。。您仍然可以使用AuthorizeView评估单个策略中的多个需求

  • 谢谢,如果我使用一个策略评估多个需求,如何根据单个需求更改文本框的属性?您是要更改单个文本框还是多个文本框?您将进行一种类型的更改还是仅进行一种类型的更改?您应该知道,如果一个策略有多个需求,所有这些需求都应该成功,以便授权服务返回successed==true。您最好定义多个策略和多个授权处理程序,并使用适当的策略和处理程序调用AuthorizationService.AuthorizationAsync,然后根据策略的评估设置文本框的属性。
     if ((await Authorize("PolicyName")).Succeeded)
     {
        ReadOnlyAttr = "readonly";
     }
    
    @inject IAuthorizationService AuthorizationService
    
    <button @onclick="@DoSomething">Do something important</button>
    
    @code {
    [CascadingParameter]
    private Task<AuthenticationState> authenticationStateTask { get; set; }
    
    private async Task DoSomething()
    {
        var user = (await authenticationStateTask).User;
    
        if ((await AuthorizationService.AuthorizeAsync(user, "CanReadNamePolicy"))
            .Succeeded)
        {
            // Perform an action only available to users satisfying the 
            // 'CanReadNamePolicy' policy.
        }
    }
    }