-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Closed
Labels
Needs: Author FeedbackThe author of this issue needs to respond in order for us to continue investigating this issue.The author of this issue needs to respond in order for us to continue investigating this issue.Status: No Recent Activityarea-authIncludes: Authn, Authz, OAuth, OIDC, BearerIncludes: Authn, Authz, OAuth, OIDC, Bearer
Description
Hi, I am trying to send custom forbid messages for different Authorization Policies in the our web api.
I followed the examples introduced in NET Core 5 for adding a custom IAuthorizationMiddlewareResultHandler.
The problem I'm facing is the object policyAuthorizationResult.AuthorizationFailure is always null so I can't tell which policy failed. The first if in the following code (my customAuthorizationMiddlewareResultHandler).
public async Task HandleAsync(
RequestDelegate requestDelegate,
HttpContext httpContext,
AuthorizationPolicy authorizationPolicy,
PolicyAuthorizationResult policyAuthorizationResult)
{
if (policyAuthorizationResult.Forbidden && policyAuthorizationResult.AuthorizationFailure != null)
{
if (policyAuthorizationResult.AuthorizationFailure.FailedRequirements.Any(requirement => requirement is LicenseRequirement))
{
var requirement = policyAuthorizationResult.AuthorizationFailure.FailedRequirements.FirstOrDefault() as LicenseRequirement;
httpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
await httpContext.Response.WriteAsync(requirement.RequirementError);
// return right away as the default implementation would overwrite the status code
return;
}
}
await _handler.HandleAsync(requestDelegate, httpContext, authorizationPolicy, policyAuthorizationResult);
}
This is how I add the Authentication middleware.
var machineKeyConfig = new XmlMachineKeyConfig(File.OpenRead("machine_config.xml"));
MachineKeyDataProtectionOptions machinekeyOptions = new MachineKeyDataProtectionOptions
{
MachineKey = new MachineKey(machineKeyConfig)
};
MachineKeyDataProtectionProvider machineKeyDataProtectionProvider = new MachineKeyDataProtectionProvider(machinekeyOptions);
MachineKeyDataProtector machineKeyDataProtector = new MachineKeyDataProtector(machinekeyOptions.MachineKey);
IDataProtector dataProtector = machineKeyDataProtector.CreateProtector("Microsoft.Owin.Security.OAuth", "Access_Token", "v1");
var ticketDataFormat = new OwinTicketDataFormat(dataProtector);
services
.AddAuthentication(o => {
o.DefaultAuthenticateScheme = OAuthValidationDefaults.AuthenticationScheme;
o.DefaultChallengeScheme = OAuthValidationDefaults.AuthenticationScheme;
})
.AddOAuthValidation(option =>
{
//using legacy ticketDataFormat for backward compatibility with older apis
option.AccessTokenFormat = ticketDataFormat;
});
Thanks
Metadata
Metadata
Assignees
Labels
Needs: Author FeedbackThe author of this issue needs to respond in order for us to continue investigating this issue.The author of this issue needs to respond in order for us to continue investigating this issue.Status: No Recent Activityarea-authIncludes: Authn, Authz, OAuth, OIDC, BearerIncludes: Authn, Authz, OAuth, OIDC, Bearer