GRPC Oauth Identity
gRPC中集成asp.net identity实现oAuth认证
在asp.net core 3.0中开启identity认证
asp.net core 3.0种需要导入的identity包与core 2.2发生了些变化:
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="3.0.0-preview5-19227-01" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.0.0-preview5-19227-01" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="3.0.0-preview5-19227-01" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.0.0-preview5-19227-01" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.0.0-preview5-19227-01" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="3.0.0-preview5.19227.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="3.0.0-preview5.19227.1" />
</ItemGroup>
代码的配置方式变化不大,主要集中在ConfigureServices中:
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); services
.AddDefaultIdentity<IdentityUser>(delegate (IdentityOptions options)
{
options.Password.RequiredLength = ;
options.Password.RequireLowercase = false;
options.Password.RequireUppercase = false;
options.Password.RequireNonAlphanumeric = false;
options.Password.RequireDigit = false;
})
.AddEntityFrameworkStores<ApplicationDbContext>(); services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
LifetimeValidator = (before, expires, token, param) => expires > DateTime.UtcNow,
ValidateAudience = false,
ValidateIssuer = false,
ValidateActor = false,
ValidateLifetime = true,
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("41B71F9E-4204-4E88-8E91-64B1981F1B82"))
};
});
在asp.net core 3.0中同时集成gRPC与Restful API
在Kestrel中同时支持HTTP1与HTTP2:
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder
.ConfigureKestrel(options =>
{
options.ListenLocalhost(, listenOptions =>
{
listenOptions.UseHttps("server.pfx", "");
listenOptions.Protocols = HttpProtocols.Http1AndHttp2;
});
})
.UseStartup<Startup>();
});
需要特别注意的一点,关于Kestrel的文档有提到,同时开启Http1与Http2需要TLS和ALPN导向HTTP/2,否则默认在HTTP1.1。
基于Bearer Token的gRPC服务授权
gRPC集成到asp.net core3.0之后,默认的授权方式可以直接使用在gRPC服务上:
[Authorize(AuthenticationSchemes = "Bearer")]
public class GreeterService : Greeter.GreeterBase
{
public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
{
return Task.FromResult(new HelloReply
{
Message = "Hello " + request.Name
});
}
}
GRPC Oauth Identity的更多相关文章
- GRPC Oauth IdentityServer4
Server端 StartUp类: using System; using System.Collections.Generic; using System.Linq; using System.Th ...
- GRPC头测试记录
GRPC头记录 http://nodejs.cn/api/http2/note_on_authority_and_host.html https://cloud.tencent.com/develop ...
- .NET Conf 2019 大会上发布.NET Core 3.0
北京时间今天凌晨如期在.NET Conf 上发布.NET Core 3.0,Keynotes 由Scott Hunter 主演,主要围绕.NET Core 3.0的新特性和社区展开. 多功能性是.Ne ...
- 使用OAuth、Identity创建WebApi认证接口供客户端调用
前言 现在的web app基本上都是前后端分离,之前接触的大部分应用场景最终产品都是部署在同一个站点下,那么随着WebApi(Restful api)的发展前后端实现的完全分离,前端不在后端框架的页面 ...
- 【ASP.NET Core分布式项目实战】(一)IdentityServer4登录中心、oauth密码模式identity server4实现
本博客根据http://video.jessetalk.cn/my/course/5视频整理 资料 OAuth2 流程:http://www.ruanyifeng.com/blog/2014/05/o ...
- Identity Server 4 预备知识 -- OAuth 2.0 简介
OAuth 2.0 简介 OAuth有一些定义: OAuth 2.0是一个委托协议, 它可以让那些控制资源的人允许某个应用以代表他们来访问他们控制的资源, 注意是代表这些人, 而不是假冒或模仿这些人. ...
- .Net Identity OAuth 2.0 SecurityStamp 使用
起源: 近期帮别人做项目,涉及到OAuth认证,服务端主动使token失效,要使对应用户不能再继续访问,只能重新登陆,或者重新授权. 场景: 这种场景OAuth2.0是支持的,比如用户修改了密码,那所 ...
- 使用ASP.NET Identity 实现WebAPI接口的Oauth身份验证
使用ASP.NET Identity 实现WebAPI接口的Oauth身份验证 目前WEB 前后端分离的开发模式比较流行,之前做过的几个小项目也都是前后分离的模式,后端使用asp.net weba ...
- ASP.NET Core分布式项目-2.oauth密码模式identity server4实现
源码下载 这里根据<ASP.NET Core分布式项目-1.IdentityServer4登录中心>的代码来继续更新oauth密码模式,这里的密码模式比上次的客户端模式更安全 在WebAp ...
随机推荐
- OpenGL+VS2012编译环境配置
OpenGL库主体分为三部分,分别是 gl(OpenGL核心库) glu(Utility Library,OpenGL实用库) glut(Utility Toolkit,OpenGL实用工具库) gl ...
- WPF模拟Office2010文件菜单的TabControl模板
原文:WPF模拟Office2010文件菜单的TabControl模板 这是Office2010中的文件菜单点开后的效果.本文我将以强大的WPF(www.itstrike.cn)来实现类似的效果.希望 ...
- sql server中使用链接服务器访问oracle数据库
一. 安装配置oracle客户端 要访问orcale数据,必须在访问的客户端机器上安装oracle客户端. Orcale有两种形式的客户端: l 完整的客户端 包括访问服务器端数据库 ...
- cross-compile-openssl-windows.sh,cross-compile-curl-windows.sh,cross-compile-zlib-windows.sh,build-zlib-visual-studio-2015-cli.bat
https://gist.github.com/artynet build zlib with Visual Studio CLI toolhttps://gist.github.com/artyne ...
- Myeclipse2014 激活 (包括方法和工具)
课程要求Myeclipse做各种各样的实验,对,当各种插头井.突然Myeclipse提示:使用过期,你可知道按那些个插件收了多少挫折么,怎能刚安好就用不了.可是又不想buy,所以就上网找破解咯,当中发 ...
- maven私服nexus安装
maven私服nexus安装 1.nexus特性 1.1.nexus私服实际上是一个javaEE的web 系统 1.2.作用:用来管理一个公司所有的jar包,实现项目jar包的版本统一 1.3.jar ...
- 微信nodejs开发模块指南
1.微信消息处理 node-weixin-message wechat 2.微信加密 node-weixin-crypto wechat-crypto 3.微信支付 node-weixin-pay w ...
- docker安装nginx并配置通过https访问
1. 下载最新的nginx的docker image docker pull nginx:latest 创建挂载路径 2.准备nginx需要的文件 nginx的配置文件 首先是nginx.conf文件 ...
- .net与.net core学习目录
.net C#调用python 模拟请求(模拟header/gzip解压/泛型) C#控制台关闭之前做一些操作 C# 元组.匿名对象.ref&out DataTable转换为Entity(反射 ...
- MVC EF Code First
1 在Models里面创建类,用[Key]特性指定主键: 2 在Model里面增加导航属性: 3 在web.config里面增加连接字符串 4 创建继承于DbContext的类 5 创建Control ...