asp.net core认证与授权:Oauth2.0概念及在.net core中的实现

为了让用户登录更方便,所以有了第三方登录,比如github,qq,淘宝等。oauth就是第三方登录的实现方案,第三方登录的原理:

  1. A 网站让用户跳转到 GitHub。
  2. GitHub 要求用户登录,然后询问"A 网站要求获得 xx 权限,你是否同意?"
  3. 用户同意,GitHub 就会重定向回 A 网站,同时发回一个授权码。
  4. A 网站使用授权码,向 GitHub 请求令牌。
  5. GitHub 返回令牌.
  6. A 网站使用令牌,向 GitHub 请求用户数据。

1、概念解析

https://www.ruanyifeng.com/blog/2019/04/oauth_design.html

2、OAuth 2.0 的四种方式

 https://www.ruanyifeng.com/blog/2019/04/oauth-grant-types.html

3、在.net core 中的实现

实现一个后端的oauth

https://www.cnblogs.com/RainingNight/p/oidc-authentication-in-asp-net-core.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
.NET Core 6实现OAuth2.0服务端,包括JWT,与.NET Core 5的实现方式基本相同。以下是一个简单的示例: 1. 创建一个ASP.NET Core Web API项目。 2. 安装IdentityServer4和Microsoft.AspNetCore.Authentication.JwtBearer NuGet包。 3. 在Startup.cs配置IdentityServer,并添加JwtBearer认证。示例代码如下: ```csharp public void ConfigureServices(IServiceCollection services) { // 添加IdentityServer服务 services.AddIdentityServer() .AddInMemoryClients(Config.Clients) .AddInMemoryApiResources(Config.Apis) .AddDeveloperSigningCredential(); // 添加JwtBearer认证 services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.Authority = "http://localhost:5000"; // IdentityServer地址 options.RequireHttpsMetadata = false; // HTTPS设置为false,方便测试 options.Audience = "api1"; // API资源名称 }); services.AddControllers(); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { app.UseRouting(); app.UseIdentityServer(); app.UseAuthentication(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } ``` 4. 创建客户端和API资源,并将它们添加到IdentityServer。示例代码如下: ```csharp public static class Config { public static IEnumerable<Client> Clients => new List<Client> { new Client { ClientId = "client1", ClientSecrets = { new Secret("secret1".Sha256()) }, AllowedGrantTypes = GrantTypes.ClientCredentials, AllowedScopes = { "api1" } } }; public static IEnumerable<ApiResource> Apis => new List<ApiResource> { new ApiResource("api1", "My API") }; } ``` 5. 实现授权终结点。示例代码如下: ```csharp [HttpPost] [Route("/connect/token")] public async Task<IActionResult> Token([FromBody] TokenRequest request) { if (request.GrantType == "password") { var user = _userService.ValidateCredentials(request.UserName, request.Password); if (user != null) { var accessToken = await _tokenService.CreateAccessTokenAsync(user); return Ok(new { access_token = accessToken, token_type = "Bearer", expires_in = (int)_tokenService.Options.Expiration.TotalSeconds }); } } return BadRequest(new { error = "unsupported_grant_type" }); } ``` 6. 实现API资源的保护。示例代码如下: ```csharp [Authorize] [HttpGet] [Route("test")] public IActionResult Test() { return Ok(new { message = "Hello, World!" }); } ``` 以上是一个基本的OAuth2.0服务端实现,包括JWT。你可以根据自己的需求进行调整和扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值