locked
Cannot implicitly convert type 'Microsoft.AspNetCore.Authorization.AuthorizationResult' to 'bool' error. RRS feed

  • Question

  • User-501297529 posted

    I get this error trying to modify the UI based on user authorization.

    This is the code on my View page where I get the error.

     }
                                        @if (await AuthorizationService.AuthorizeAsync(User, "EditDiscussions"))
                                        {
                                            <li class="nav-item">
                                                <a class="nav-link"
                                                   asp-controller="Home"
                                                   asp-action="Directors">Directors</a>
                                            </li>
                                        }

    This is what I have on retrieving an instance of IAuthorizationService. I tried replacing <AuthorizationResult> with <bool> but that didn't work.

      public interface IAuthorizationService
        {
            Task<AuthorizationResult> AuthorizeAsync(ClaimsPrincipal user, object resource, IEnumerable<IAuthorizationRequirement> requirements);
    
            
            Task<AuthorizationResult> AuthorizeAsync(ClaimsPrincipal user, object resource, string policyName);
        }

    Thursday, February 27, 2020 12:24 AM

Answers

  • User-1716253493 posted

    try

    @if (await AuthorizationService.AuthorizeAsync(User, "EditDiscussions").Succeeded)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 27, 2020 12:38 AM

All replies

  • User-1716253493 posted

    try

    @if (await AuthorizationService.AuthorizeAsync(User, "EditDiscussions").Succeeded)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 27, 2020 12:38 AM
  • User-501297529 posted

    oned_gk

    try

    @if (await AuthorizationService.AuthorizeAsync(User, "EditDiscussions").Succeeded)

    Tried this and it I get ' 'Task<AuthorizationResult>' does not contain a definition for 'Succeeded' and no accessible extension method 'Succeeded' accepting a first argument of type 'Task<AuthorizationResult>' could be found (are you missing a using directive or an assembly reference?)' error. Even though in the AuthorizationResult class, Succeeded is in there. This is what that class looks like.

    public class AuthorizationResult
        {
            private AuthorizationResult() { }
           
            public bool Succeeded { get; private set; }
          
            public AuthorizationFailure Failure { get; private set; }
           
            public static AuthorizationResult Success() => new AuthorizationResult { Succeeded = true };
    
            public static AuthorizationResult Failed(AuthorizationFailure failure) => new AuthorizationResult { Failure = failure };
    
            public static AuthorizationResult Failed() => new AuthorizationResult { Failure = AuthorizationFailure.ExplicitFail() };
    
        }

    Also try this by removing the await

    @if (AuthorizationService.AuthorizeAsync(User, "EditDiscussions").Result.Succeeded)                   

    and don't get an error but it doesn't hit the role successfully. Meaning when my role is authorized to Edit Discussions I can't see the link on the view . When I debug it doesn't go into this code.

                                          @if (AuthorizationService.AuthorizeAsync(User, "EditDiscussions").Result.Succeeded) 
    {
    <li class="nav-item"> <a class="nav-link" asp-controller="Home" asp-action="Directors">Directors</a> </li>
    }

    Thursday, February 27, 2020 1:52 PM