GRPC Oauth IdentityServer4
Server端
StartUp类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using IdentityServer4.AccessTokenValidation;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; namespace GRPCTokenServer
{
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
//services.AddHttpContextAccessor(); services.AddGrpc(options => { options.EnableDetailedErrors = true; });
services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
.AddIdentityServerAuthentication(options =>
{
options.Authority = "http://localhost:54311/";
options.RequireHttpsMetadata = false; options.ApiName = "identity";
options.SaveToken = true;
});
services
.AddControllers();
} // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting(); app.UseAuthentication();
app.UseAuthorization(); app.UseEndpoints(endpoints =>
{
// Communication with gRPC endpoints must be made through a gRPC client.
// To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909
endpoints.MapGrpcService<GreeterService>();
//endpoints.MapControllers();
});
}
}
}
Service
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Grpc.Core;
using Microsoft.AspNetCore.Authorization; namespace GRPCTokenServer
{
[Authorize(AuthenticationSchemes = "Bearer")]
public class GreeterService : Greeter.GreeterBase
{
public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
{
var user = context.GetHttpContext().User;
return Task.FromResult(new HelloReply
{
Message = "Hello " + request.Name
});
}
}
}
proto
syntax = "proto3"; option csharp_namespace = "GRPCTokenServer"; package Greet; // The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
} // The request message containing the user's name.
message HelloRequest {
string name = ;
} // The response message containing the greetings.
message HelloReply {
string message = ;
}
Client
using Grpc.Core;
using Grpc.Net.Client;
using GRPCTokenServer;
using System;
using System.Net.Http; namespace GRPCTokenClient
{
class Program
{
static async System.Threading.Tasks.Task Main(string[] args)
{
// AppContext.SetSwitch(
//"System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport",
//true);
// var httpClient = new HttpClient();
// // The port number(50051) must match the port of the gRPC server.
// httpClient.BaseAddress = new Uri("http://localhost:50051");
// var client = GrpcClient.Create<Greeter.GreeterClient>(httpClient); // HttpClient httpClient = new HttpClient();
//httpClient.BaseAddress = new Uri("https://localhost:50051");
//var result = await httpClient.PostAsync("api/token", new { Email = "admin@contract.com", Password = "12345678" }.AsJson());
var tokenValue = "Bearer " + "eyJhbGciOiJSUzI1NiIsImtpZCI6Ijk4OTIzRkRERTkxODJDOURERjRGQzZCQzNBMEI1RDUzNDNFNkM4QjEiLCJ0eXAiOiJKV1QiLCJ4NXQiOiJtSklfM2VrWUxKM2ZUOGE4T2d0ZFUwUG15TEUifQ.eyJuYmYiOjE1NjEyNjEwODUsImV4cCI6MTU2MjI2MTA4NSwiaXNzIjoiaHR0cDovL2xvY2FsaG9zdDo1NDMxMSIsImF1ZCI6ImlkZW50aXR5IiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvbmFtZWlkZW50aWZpZXIiOiI0NzczYzUzMi0wMDg1LTQwZGUtYTllNy1iZTNlMjBhNjdlOTQiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoiMTU5MDE0MDIzODIiLCJBc3BOZXQuSWRlbnRpdHkuU2VjdXJpdHlTdGFtcCI6Iks0RlE2Sk1TNEZPUzROR1Q2TE9UVFlJR1ZDRTZRQlRVIiwidXNlcmlkIjoiNDc3M2M1MzItMDA4NS00MGRlLWE5ZTctYmUzZTIwYTY3ZTk0IiwidXNlcm5hbWUiOiIxNTkwMTQwMjM4MiJ9.pSEkPwyRMNeDYd6ONR0xjJMfhFhOgZB_gcr0fa7NP8dAnPfuf4aW0xIzNsAp6NGn91fu9vbV5gSEbTUghRfzKemEcPwIDaeho1oYvV-xFRWBBo4JFBx5FcB-kVdy4TeFCTu1nTIb0MUqmkgk40HFngmK7jW9epAu2m1YYvyvweqoe5cS4eHcEMun4lSOlJwoCmL-V1DW_LQb8LojrBUjn2mz3f0yAlUWIA_vi_Z37QX60Sg-BMtlrH0fdaJuypNdRtlWp6qvNEZgZ496wIjHnSCUr15Z6AbqQfa2XTBI16pLj96HTeTjkxGR0XmoCaRmXWiTeOg0nFq5pZ8dDoJOIg"; var metadata = new Metadata
{
{ "Authorization", tokenValue }
};
CallOptions callOptions = new CallOptions(metadata); var channel = new Channel("localhost:50051", SslCredentials.Insecure); var client = new Greeter.GreeterClient(channel); var reply = await client.SayHelloAsync(
new HelloRequest { Name = "GreeterClient" }, callOptions);
Console.WriteLine("Greeting: " + reply.Message);
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}
}
https://github.com/cysnet/Secure_gRpc
https://github.com/cysnet/GRPC_IdentityServer4
https://damienbod.com/2019/03/06/security-experiments-with-grpc-and-asp-net-core-3-0/
GRPC Oauth IdentityServer4的更多相关文章
- .Net Core3.0使用gRPC 和IdentityServer4
gRPC是什么gRPC是可以在任何环境中运行的现代开源高性能RPC框架.它可以通过可插拔的支持来有效地连接数据中心内和跨数据中心的服务,以实现负载平衡,跟踪,运行状况检查和身份验证.它也适用于分布式计 ...
- GRPC Oauth Identity
gRPC中集成asp.net identity实现oAuth认证 在asp.net core 3.0中开启identity认证 asp.net core 3.0种需要导入的identity包与core ...
- .net core gRPC与IdentityServer4集成认证授权
前言 随着.net core3.0的正式发布,gRPC服务被集成到了VS2019.本文主要演示如何对gRPC的服务进行认证授权. 分析 目前.net core使用最广的认证授权组件是基于OAuth2. ...
- IdentityServer4 ASP.NET Core的OpenID Connect OAuth 2.0框架学习保护API
IdentityServer4 ASP.NET Core的OpenID Connect OAuth 2.0框架学习之保护API. 使用IdentityServer4 来实现使用客户端凭据保护ASP.N ...
- IdentityServer4 实现 OpenID Connect 和 OAuth 2.0
关于 OAuth 2.0 的相关内容,点击查看:ASP.NET WebApi OWIN 实现 OAuth 2.0 OpenID 是一个去中心化的网上身份认证系统.对于支持 OpenID 的网站,用户不 ...
- IdentityServer4 实现 OAuth 2.0(密码模式 - HTTP Post 方式)
之前写了一篇文章:<IdentityServer4 实现 OpenID Connect 和 OAuth 2.0> 上面这篇文章虽然详细,但都是点到为止的介绍,并没有实际应用的示例,所以,后 ...
- 【ASP.NET Core分布式项目实战】(一)IdentityServer4登录中心、oauth密码模式identity server4实现
本博客根据http://video.jessetalk.cn/my/course/5视频整理 资料 OAuth2 流程:http://www.ruanyifeng.com/blog/2014/05/o ...
- 使用 IdentityServer4 实现 OAuth 2.0 与 OpenID Connect 服务
IdentityServer4 是 ASP.NET Core 的一个包含 OIDC 和 OAuth 2.0 协议的框架.最近的关注点在 ABP 上,默认 ABP 也集成 IdentityServer4 ...
- 简单理解 OAuth 2.0 及资料收集,IdentityServer4 部分源码解析
简单理解 OAuth 2.0 及资料收集,IdentityServer4 部分源码解析 虽然经常用 OAuth 2.0,但是原理却不曾了解,印象里觉得很简单,请求跳来跳去,今天看完相关介绍,就来捋一捋 ...
随机推荐
- 在wpf datagrid中,想要根据一个条件来改变datagrid行的背景颜色
原文:在wpf datagrid中,想要根据一个条件来改变datagrid行的背景颜色 在wpf datagrid中,想要根据一个条件来改变datagrid行的背景颜色 例如根据学生的年龄来修改,年龄 ...
- python解决urllib2乱码问题
示例: #!/usr/bin/env python # -*- coding: utf-8 -*- import urllib import urllib2 def main(): url = &qu ...
- x:Static , StaticResource 和DynamicResource等XAML 扩展用法
原文:x:Static , StaticResource 和DynamicResource等XAML 扩展用法 前提: <system:String x:Key="{Component ...
- Angular使用echarts
安装 npm install echarts --save npm install @types/echarts --save 基本使用 定义一个dom <div id="chart& ...
- iOS_9_scrollView分页
最后效果图: BeyondViewController.h // // BeyondViewController.h // 8_scrollVIew分页浏览 // // Created by beyo ...
- ListView、TreeView和DataGrid。
原文:ListView.TreeView和DataGrid. 1.ListView. ListView继承自简单的没有特色的ListBox,并使用View属性进行扩展.增加了对基于列显示的支持,并增加 ...
- IOS开发之iOS深浅拷贝
这里主要侧重于集合类的深浅拷贝,主要事因为工作的时候遇到这个问题. 有不足的地方欢迎指正 首先我们需要有这样的一个前提: [array addObject:obj]; 这样obj的引用计数会增加1,如 ...
- epplus输出成thml
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- Resolve conflict using "MERGE_HEAD (origin/HEAD)"
Git进行同步的时候,经常会出现冲突,有时候冲突的选项会有图示中的三种选项: 1.Resolved:直接把文件标识为冲突已经解决,一般是自己手动查看并解决完冲突以后使用. 2.Resolve conf ...
- C# winform 主界面打开并关闭登录界面
在winform 界面编程中,我们有时候要在主界面打开之前先显示登录界面,当登录界面用户信息校验正确后才打开主界面,而这时登陆界面也完成使命该功成身退了. 目前有两种方法可实现: 方法1. 隐藏登录界 ...