Skip to content

Commit

Permalink
Example (protobuf-net#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
Euan-McVie committed Sep 21, 2020
1 parent 91e6640 commit d5cdf87
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
33 changes: 33 additions & 0 deletions examples/pb-net-grpc/Server_CS/FakeAuthenticationHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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<FakeAuthenticationSchemeOptions>
{
public const string SchemeName = "Fake";

public FakeAuthHandler(
IOptionsMonitor<FakeAuthenticationSchemeOptions> options,
ILoggerFactory logger,
UrlEncoder encoder,
ISystemClock clock)
: base(options, logger, encoder, clock)
{
}

protected override Task<AuthenticateResult> HandleAuthenticateAsync()
{
var claimsIdentity = new ClaimsIdentity(SchemeName);
var ticket = new AuthenticationTicket(new ClaimsPrincipal(claimsIdentity), Scheme.Name);
return Task.FromResult(AuthenticateResult.Success(ticket));
}
}

class FakeAuthenticationSchemeOptions : AuthenticationSchemeOptions
{ }
}
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);
}
}
}
9 changes: 7 additions & 2 deletions examples/pb-net-grpc/Server_CS/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@ public void ConfigureServices(IServiceCollection services)
services.AddCodeFirstGrpc(config =>
{
config.ResponseCompressionLevel = System.IO.Compression.CompressionLevel.Optimal;
});
})
.WithBinderConfiguration(config => config.ServiceBinder = new ServiceBinderWithServiceResolutionFromServiceCollection(services));
services.AddCodeFirstGrpcReflection();

services.AddAuthentication();
services.AddAuthentication(options =>
{
options.AddScheme<FakeAuthHandler>(FakeAuthHandler.SchemeName, FakeAuthHandler.SchemeName);
options.DefaultScheme = FakeAuthHandler.SchemeName;
});
services.AddAuthorization();
services.AddSingleton<ICounter, MyCounter>();
}
Expand Down

0 comments on commit d5cdf87

Please sign in to comment.