-
-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for defining a custom metadata lookup in the servicebinder (
#121) (#138) * Move `GetMetadata` to the `ServiceBinder` to allow override (#121) * Add singleton with `Authorize` attribute example (#121)
- Loading branch information
1 parent
786f359
commit e7d5887
Showing
10 changed files
with
229 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
examples/pb-net-grpc/Server_CS/FakeAuthenticationHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using System.Security.Claims; | ||
using System.Text.Encodings.Web; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Authentication; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
|
||
namespace Server_CS | ||
{ | ||
class FakeAuthHandler : AuthenticationHandler<FakeAuthOptions> | ||
{ | ||
public const string SchemeName = "Fake"; | ||
|
||
public FakeAuthHandler( | ||
IOptionsMonitor<FakeAuthOptions> options, | ||
ILoggerFactory logger, | ||
UrlEncoder encoder, | ||
ISystemClock clock) | ||
: base(options, logger, encoder, clock) | ||
{ | ||
} | ||
|
||
protected override Task<AuthenticateResult> HandleAuthenticateAsync() | ||
{ | ||
if (!Options.AlwaysAuthenticate) | ||
return Task.FromResult(AuthenticateResult.NoResult()); | ||
|
||
var claimsIdentity = new ClaimsIdentity(SchemeName); | ||
var ticket = new AuthenticationTicket(new ClaimsPrincipal(claimsIdentity), Scheme.Name); | ||
return Task.FromResult(AuthenticateResult.Success(ticket)); | ||
} | ||
} | ||
|
||
class FakeAuthOptions : AuthenticationSchemeOptions | ||
{ | ||
public bool AlwaysAuthenticate { get; set; } = false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Shared_CS; | ||
|
||
namespace Server_CS | ||
{ | ||
[Authorize] | ||
public class MyCounter : ICounter | ||
{ | ||
private int counter = 0; | ||
private readonly object counterLock = new object(); | ||
|
||
ValueTask<IncrementResult> ICounter.IncrementAsync(IncrementRequest request) | ||
{ | ||
lock (counterLock) | ||
{ | ||
counter += request.Inc; | ||
var result = new IncrementResult { Result = counter }; | ||
return new ValueTask<IncrementResult>(result); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
examples/pb-net-grpc/Server_CS/ServiceBinderWithServiceResolutionFromServiceCollection.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Reflection; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using ProtoBuf.Grpc.Configuration; | ||
|
||
namespace Server_CS | ||
{ | ||
internal class ServiceBinderWithServiceResolutionFromServiceCollection : ServiceBinder | ||
{ | ||
private readonly IServiceCollection services; | ||
|
||
public ServiceBinderWithServiceResolutionFromServiceCollection(IServiceCollection services) | ||
{ | ||
this.services = services; | ||
} | ||
|
||
public override IList<object> GetMetadata(MethodInfo method, Type contractType, Type serviceType) | ||
{ | ||
var resolvedServiceType = serviceType; | ||
if (serviceType.IsInterface) | ||
resolvedServiceType = services.SingleOrDefault(x => x.ServiceType == serviceType)?.ImplementationType ?? serviceType; | ||
|
||
return base.GetMetadata(method, contractType, resolvedServiceType); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
using System.Runtime.Serialization; | ||
using System.ServiceModel; | ||
using System.Threading.Tasks; | ||
namespace Shared_CS | ||
{ | ||
[ServiceContract] | ||
public interface ICounter | ||
{ | ||
ValueTask<IncrementResult> IncrementAsync(IncrementRequest request); | ||
} | ||
|
||
[DataContract] | ||
public class IncrementRequest | ||
{ | ||
[DataMember(Order = 1)] | ||
public int Inc { get; set; } | ||
} | ||
|
||
[DataContract] | ||
public class IncrementResult | ||
{ | ||
[DataMember(Order = 1)] | ||
public int Result { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.