Skip to content

Commit 435a98c

Browse files
committed
fix: 修正遺漏屬性
1 parent 5a80478 commit 435a98c

5 files changed

Lines changed: 22 additions & 10 deletions

File tree

Helpers/ContextStateHelper.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Security.Claims;
2+
using Microsoft.Net.Http.Headers;
23
using Netcorext.Contracts;
34

45
namespace Netcorext.Extensions.Grpc.Helpers;
@@ -7,7 +8,7 @@ public static class ContextStateHelper
78
{
89
public static string? GetAuthorizationToken(this IContextState context, IHeaderDictionary? headers)
910
{
10-
if (headers?.TryGetValue("Authorization", out var authorization) == true && !string.IsNullOrWhiteSpace(authorization))
11+
if (headers?.TryGetValue(HeaderNames.Authorization, out var authorization) == true && !string.IsNullOrWhiteSpace(authorization))
1112
return authorization;
1213
if (context.User?.Identity == null)
1314
return null;

Interceptors/AuthorizationHeaderInterceptor.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Grpc.Core;
22
using Grpc.Core.Interceptors;
3+
using Microsoft.Net.Http.Headers;
34
using Netcorext.Contracts;
45
using Netcorext.Extensions.Grpc.Helpers;
56

@@ -25,7 +26,7 @@ public override AsyncUnaryCall<TResponse> AsyncUnaryCall<TRequest, TResponse>(TR
2526
if (string.IsNullOrWhiteSpace(authorization))
2627
return continuation(request, context);
2728

28-
var metadata = new Metadata { { "Authorization", $"{authorization}" } };
29+
var metadata = new Metadata { { HeaderNames.Authorization, $"{authorization}" } };
2930
var options = context.Options.WithHeaders(metadata);
3031
var newContext = new ClientInterceptorContext<TRequest, TResponse>(context.Method, context.Host, options);
3132

@@ -39,7 +40,7 @@ public override TResponse BlockingUnaryCall<TRequest, TResponse>(TRequest reques
3940
if (string.IsNullOrWhiteSpace(authorization))
4041
return continuation(request, context);
4142

42-
var metadata = new Metadata { { "Authorization", $"{authorization}" } };
43+
var metadata = new Metadata { { HeaderNames.Authorization, $"{authorization}" } };
4344
var options = context.Options.WithHeaders(metadata);
4445
var newContext = new ClientInterceptorContext<TRequest, TResponse>(context.Method, context.Host, options);
4546

Interceptors/OriginHeaderPassingInterceptor.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using Grpc.Core;
22
using Grpc.Core.Interceptors;
3+
using Microsoft.Extensions.Primitives;
4+
using Microsoft.Net.Http.Headers;
35
using Netcorext.Contracts;
46
using Netcorext.Extensions.Grpc.Helpers;
57
using Netcorext.Extensions.Grpc.Options;
@@ -26,11 +28,15 @@ public override AsyncUnaryCall<TResponse> AsyncUnaryCall<TRequest, TResponse>(TR
2628
var entries = _httpContextAccessor.HttpContext?.Request.Headers
2729
.Where(_options.Handler)
2830
.Select(t => new Metadata.Entry(t.Key.ToLower(), t.Value))
29-
.ToArray();
31+
.ToList() ?? new List<Metadata.Entry>();
3032

3133
var authorization = _contextState.GetAuthorizationToken(_httpContextAccessor.HttpContext?.Request.Headers);
34+
var authorizationHeader = new KeyValuePair<string, StringValues>(HeaderNames.Authorization, authorization);
3235

33-
if (entries?.Any() != true)
36+
if (_options.Handler(authorizationHeader))
37+
entries.Add(new Metadata.Entry(authorizationHeader.Key, authorizationHeader.Value));
38+
39+
if (entries.Count == 0)
3440
return continuation(request, context);
3541

3642
var metadata = new Metadata();
@@ -52,11 +58,15 @@ public override TResponse BlockingUnaryCall<TRequest, TResponse>(TRequest reques
5258
var entries = _httpContextAccessor.HttpContext?.Request.Headers
5359
.Where(_options.Handler)
5460
.Select(t => new Metadata.Entry(t.Key.ToLower(), t.Value))
55-
.ToArray();
61+
.ToList() ?? new List<Metadata.Entry>();
5662

5763
var authorization = _contextState.GetAuthorizationToken(_httpContextAccessor.HttpContext?.Request.Headers);
64+
var authorizationHeader = new KeyValuePair<string, StringValues>(HeaderNames.Authorization, authorization);
65+
66+
if (_options.Handler(authorizationHeader))
67+
entries.Add(new Metadata.Entry(authorizationHeader.Key, authorizationHeader.Value));
5868

59-
if (entries?.Any() != true)
69+
if (entries.Count == 0)
6070
return continuation(request, context);
6171

6272
var metadata = new Metadata();

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2024 NETCOREXT
3+
Copyright (c) 2025 NETCOREXT
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
using Grpc.Core;
21
using Microsoft.Extensions.Primitives;
2+
using Microsoft.Net.Http.Headers;
33

44
namespace Netcorext.Extensions.Grpc.Options;
55

66
public class OriginHeaderPassingInterceptorOptions
77
{
8-
public Func<KeyValuePair<string, StringValues>, bool> Handler { get; set; } = entry => entry.Key == "Authorization";
8+
public Func<KeyValuePair<string, StringValues>, bool> Handler { get; set; } = entry => entry.Key == HeaderNames.Authorization;
99
}

0 commit comments

Comments
 (0)