Merge pull request #9231 from abpframework/maliming/AllowAnonymous

Skip authenticate when action allow anonymous.
pull/9295/head^2
Halil İbrahim Kalkan 4 years ago committed by GitHub
commit 7574dc088d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -4,6 +4,7 @@ using System.Linq;
using System.Reflection; using System.Reflection;
using System.Text; using System.Text;
using JetBrains.Annotations; using JetBrains.Annotations;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Abstractions; using Microsoft.AspNetCore.Mvc.Abstractions;
using Microsoft.AspNetCore.Mvc.ApiExplorer; using Microsoft.AspNetCore.Mvc.ApiExplorer;
@ -102,6 +103,16 @@ namespace Volo.Abp.AspNetCore.Mvc
Logger.LogDebug($"ActionApiDescriptionModel.Create: {controllerModel.ControllerName}.{uniqueMethodName}"); Logger.LogDebug($"ActionApiDescriptionModel.Create: {controllerModel.ControllerName}.{uniqueMethodName}");
bool? allowAnonymous = null;
if (apiDescription.ActionDescriptor.EndpointMetadata.Any(x => x is IAllowAnonymous))
{
allowAnonymous = true;
}
else if (apiDescription.ActionDescriptor.EndpointMetadata.Any(x => x is IAuthorizeData))
{
allowAnonymous = false;
}
var actionModel = controllerModel.AddAction( var actionModel = controllerModel.AddAction(
uniqueMethodName, uniqueMethodName,
ActionApiDescriptionModel.Create( ActionApiDescriptionModel.Create(
@ -109,7 +120,8 @@ namespace Volo.Abp.AspNetCore.Mvc
method, method,
apiDescription.RelativePath, apiDescription.RelativePath,
apiDescription.HttpMethod, apiDescription.HttpMethod,
GetSupportedVersions(controllerType, method, setting) GetSupportedVersions(controllerType, method, setting),
allowAnonymous
) )
); );

@ -156,14 +156,17 @@ namespace Volo.Abp.Http.Client.DynamicProxying
AddHeaders(invocation, action, requestMessage, apiVersion); AddHeaders(invocation, action, requestMessage, apiVersion);
await ClientAuthenticator.Authenticate( if (action.AllowAnonymous != true)
new RemoteServiceHttpClientAuthenticateContext( {
client, await ClientAuthenticator.Authenticate(
requestMessage, new RemoteServiceHttpClientAuthenticateContext(
remoteServiceConfig, client,
clientConfig.RemoteServiceName requestMessage,
) remoteServiceConfig,
); clientConfig.RemoteServiceName
)
);
}
var response = await client.SendAsync( var response = await client.SendAsync(
requestMessage, requestMessage,

@ -26,12 +26,14 @@ namespace Volo.Abp.Http.Modeling
public ReturnValueApiDescriptionModel ReturnValue { get; set; } public ReturnValueApiDescriptionModel ReturnValue { get; set; }
public bool? AllowAnonymous { get; set; }
public ActionApiDescriptionModel() public ActionApiDescriptionModel()
{ {
} }
public static ActionApiDescriptionModel Create([NotNull] string uniqueName, [NotNull] MethodInfo method, [NotNull] string url, [CanBeNull] string httpMethod, [NotNull] IList<string> supportedVersions) public static ActionApiDescriptionModel Create([NotNull] string uniqueName, [NotNull] MethodInfo method, [NotNull] string url, [CanBeNull] string httpMethod, [NotNull] IList<string> supportedVersions, bool? allowAnonymous = null)
{ {
Check.NotNull(uniqueName, nameof(uniqueName)); Check.NotNull(uniqueName, nameof(uniqueName));
Check.NotNull(method, nameof(method)); Check.NotNull(method, nameof(method));
@ -50,7 +52,8 @@ namespace Volo.Abp.Http.Modeling
.GetParameters() .GetParameters()
.Select(MethodParameterApiDescriptionModel.Create) .Select(MethodParameterApiDescriptionModel.Create)
.ToList(), .ToList(),
SupportedVersions = supportedVersions SupportedVersions = supportedVersions,
AllowAnonymous = allowAnonymous
}; };
} }

Loading…
Cancel
Save