为了账号安全,请及时绑定邮箱和手机立即绑定

asp.net core2.2 迁移到 asp.net core3.0

标签:
C#

asp.net core2.2 迁移到 asp.net core3.0

认证 Authentication、授权 Authorization 和身份标识 Identity

发出 HTTP 请求 HttpClientFactory

OAuth 使用 System.Txt.Json

  • OAuthHandler 中请求结果读取字符串后使用的序列化组件改成了 System.Txt.Json,相关类参数也换成了 JsonDocument 或者 JsonElement
var payload = JsonDocument.Parse(await response.Content.ReadAsStringAsync());
var user = payload.RootElement;

SpaServices

跨域

  • 不再允许最大限度跨域,以下代码运行报错
            // 跨域
            app.UseCors(config =>
                config.AllowAnyHeader()
                    .AllowAnyMethod()
                    .AllowAnyOrigin()
                    .AllowCredentials());
  • 改为使用默认的策略
app.UseCors("default");

swagger 升级

  • swagger 使用的是 Swashbuckle.AspNetCore,目前版本是版本是 5.0.0-rc4,这个正式版还没出,这个版本就是为了适配 dotnetcore 3.0 的,整体变化不大,部分设置修改为了类
  • 首先是 api 描述有 Info 换成了 OpenApiInfo,引入 Microsoft.OpenApi.Models 命名空间
  • 下面是认证设置,先是 4.x 旧版本
                c.AddSecurityDefinition("Bearer", new ApiKeyScheme
                {
                    Description = "JWT Authorization",
                    Name = "Authorization",
                    In = "header",
                    Type = "apiKey"
                });

                c.AddSecurityRequirement(new Dictionary<string, IEnumerable<string>>
                {
                    { "Bearer",new string[]{}}
                });
  • 5.x 版本
                var bearerScheme = new OpenApiSecurityScheme
                {
                    Description = "JWT Authorization",
                    Name = "Authorization",
                    In = ParameterLocation.Header,
                    Type = SecuritySchemeType.ApiKey
                };

                c.AddSecurityDefinition("Bearer", bearerScheme);

                c.AddSecurityRequirement(new OpenApiSecurityRequirement()
                {
                    { bearerScheme, new List<string>()}

                });
  • 然后是 OAuth 设置,4.x 版本
                    c.AddSecurityDefinition("oauth2", new OAuth2Scheme
                    {
                        Type = "oauth2",
                        Flow = "authorizationCode",
                        TokenUrl = "/api/Account/GetToken",
                        AuthorizationUrl = oAuthConfiguration.Authority.EnsureEnd("/") + "connect/authorize",
                        Description = "OAuth2登陆授权",
                        Scopes = new Dictionary<string, string>
                        {
                            { "api1", "测试"},
                            { "openid","唯一标识" },
                            { "profile","用户信息"}
                        }
                    });

                    c.AddSecurityRequirement(new Dictionary<string, Enumerable<string>>
                    {
                        { "oauth2",new string[]{}}
                    });
  • 5.x 版本
var oauth2Scheme = new OpenApiSecurityScheme
                    {
                        Type = SecuritySchemeType.OAuth2,
                        Description = "OAuth2登陆授权",
                        Flows = new OpenApiOAuthFlows()
                        {
                            AuthorizationCode = new OpenApiOAuthFlow()
                            {
                                AuthorizationUrl = new Uri(oAuthConfiguration.Authority.EnsureEnd("/") + "connect/authorize"),
                                TokenUrl = new Uri("/api/Account/GetToken",UriKind.Relative),
                                Scopes = new Dictionary<string, string>
                                {
                                    {"api1", "测试"},
                                    {"openid", "唯一标识"},
                                    {"profile", "用户信息"}
                                }
                            }
                        }
                    };

                    c.AddSecurityDefinition("oauth2", oauth2Scheme);
                    c.AddSecurityRequirement(new OpenApiSecurityRequirement
                    {
                        { oauth2Scheme, new List<string>(){"openid"}}
                    });

未完待续

  • 后续遇到再补充
点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消