Sample CustomPolicyProvider throws "Nullability of reference types" warning on .net 6

M.H. Mukit 26 Reputation points
2022-09-14T15:56:11.153+00:00

I am trying to implement custom policy provider on .net 6(c#10). Due to some changes on c#10 compiler throws some warning for the following sample implmentation. Please provide a different implementation to remove the warnings.

https://github.com/dotnet/aspnetcore/blob/v3.1.3/src/Security/samples/CustomPolicyProvider/Authorization/MinimumAgePolicyProvider.cs

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,158 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rena Ni - MSFT 2,061 Reputation points
    2022-09-15T05:39:09.86+00:00

    Hi @M.H. Mukit ,

    You can F12 to step to the source code of IAuthorizationPolicyProvider in Visual Studio and you can see the two method of the response data is nullable. You can add ? to allow nullable. Here is a whole working sample you could follow:

    internal class MinimumAgePolicyProvider : IAuthorizationPolicyProvider  
    {  
        const string POLICY_PREFIX = "MinimumAge";  
        public DefaultAuthorizationPolicyProvider FallbackPolicyProvider { get; }  
      
        public MinimumAgePolicyProvider(IOptions<AuthorizationOptions> options)  
        {  
            // ASP.NET Core only uses one authorization policy provider, so if the custom implementation  
            // doesn't handle all policies (including default policies, etc.) it should fall back to an  
            // alternate provider.  
            //  
            // In this sample, a default authorization policy provider (constructed with options from the   
            // dependency injection container) is used if this custom provider isn't able to handle a given  
            // policy name.  
            //  
            // If a custom policy provider is able to handle all expected policy names then, of course, this  
            // fallback pattern is unnecessary.  
            FallbackPolicyProvider = new DefaultAuthorizationPolicyProvider(options);  
        }  
      
        public Task<AuthorizationPolicy> GetDefaultPolicyAsync() => FallbackPolicyProvider.GetDefaultPolicyAsync();  
      
        public Task<AuthorizationPolicy?> GetFallbackPolicyAsync() => FallbackPolicyProvider.GetFallbackPolicyAsync();  
      
        // Policies are looked up by string name, so expect 'parameters' (like age)  
        // to be embedded in the policy names. This is abstracted away from developers  
        // by the more strongly-typed attributes derived from AuthorizeAttribute  
        // (like [MinimumAgeAuthorize] in this sample)  
        public Task<AuthorizationPolicy?> GetPolicyAsync(string policyName)  
        {  
            if (policyName.StartsWith(POLICY_PREFIX, StringComparison.OrdinalIgnoreCase) &&  
                int.TryParse(policyName.Substring(POLICY_PREFIX.Length), out var age))  
            {  
                var policy = new AuthorizationPolicyBuilder();  
                policy.AddRequirements(new MinimumAgeRequirement(age));  
                return Task.FromResult(policy?.Build());  
            }  
      
            // If the policy name doesn't match the format expected by this policy provider,  
            // try the fallback provider. If no fallback provider is used, this would return   
            // Task.FromResult<AuthorizationPolicy>(null) instead.  
            return FallbackPolicyProvider.GetPolicyAsync(policyName);  
        }  
    }  
    

    Here is also another way which can keep the previous code, you can remove the <Nullable>enable</Nullable> in your project file.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best Regards,
    Rena

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. M.H. Mukit 26 Reputation points
    2022-09-15T19:11:13.363+00:00

    Hi @Rena Ni - MSFT

    Thank you for the suggestion. I did some mistakes previously. I used your advice accordingly, and it removed the warning from occurring.. Thanks again.

    1 person found this answer helpful.
    0 comments No comments