简体   繁体   English

使用 OWIN 从 asp.net 核心发布的 oAuth 承载

[英]consuming oAuth bearer issued by OWIN from asp.net core

I have ASP.NET 4.5 service with OWIN pipeline that issues OAuth access_token in exchange for username/password.我有带有 OWIN 管道的 ASP.NET 4.5 服务,它发出 OAuth access_token 以换取用户名/密码。 This service is called from ng-app and once it gets a token, stores in the browsers local storage.该服务从 ng-app 调用,一旦它获得令牌,就会存储在浏览器的本地存储中。 Then it calls resource api that consumes this token, API is also written in asp.net 4.5 and uses owin.然后调用消耗这个token的资源API,API也是用asp.net 4.5写的,用的是owin。 This being OAuth token issued with OWIN it's encrypted/signed to machineKey secrets - so far so good and is happily consumed by the resource API.这是与 OWIN 一起发布的 OAuth 令牌,它已加密/签名为 machineKey 机密 - 到目前为止一切顺利,并且被资源 API 愉快地使用。 All this made possible by OAuthAuthorizationServerMiddleware.所有这一切都是由 OAuthAuthorizationServerMiddleware 实现的。

Now I need to consume these tokens sent from the same ng-app to asp.net core 2.1 services, nothing fancy, just verify/decode it and get claims inside the token.现在我需要使用从同一个 ng-app 发送到 asp.net core 2.1 服务的这些令牌,没什么特别的,只需验证/解码它并在令牌中获取声明。
This OAuthAuthorizationServerMiddleware was never ported to asp.net core so I am stuck.这个 OAuthAuthorizationServerMiddleware 从未移植到 asp.net 核心,所以我被卡住了。 ( OAuth Authorization Service in ASP.NET Core does not help it talks about the full-fledged oidc server I just need to consume them w/o changing the issuing code) In ConfigureServices() tacked on to service.AddAuthentication(): Tried.AddJwtBearer - but this makes no sense - these are not Jwt tokens really Tried.AddOAuth but this does not make sense either b/c I am not dealing with full OAuth flow with redirects to obtain a token, I also don't deal with ClientId/ClientSecret/etc, I just receive "Bearer token-here" in the HTTP header from ng app so I need something in the pipeline to decode this and set ClaimsIdentity but this "something in the pipeline" also needs to have access to machinery-like data that is the same as it is in asp.net 4.5 OWIN service Any ideas? ASP.NET Core中的OAuth 授权服务并不能帮助它谈论成熟的 oidc 服务器,我只需要在不更改发布代码的情况下使用它们)在 ConfigureServices() 中附加到 service.AddAuthentication():尝试过。 AddJwtBearer - 但这没有意义 - 这些不是真正的 Jwt 令牌 Tried.AddOAuth 但这也没有意义 b/c 我没有处理带有重定向的完整 OAuth 流以获得令牌,我也不处理 ClientId /ClientSecret/etc,我只是在来自 ng 应用程序的 HTTP 标头中收到“Bearer token-here”,所以我需要管道中的一些东西来解码它并设置 ClaimsIdentity 但是这个“管道中的东西”也需要访问机器 -像在asp.net 4.5 OWIN服务中一样的数据有什么想法吗?

You could set the OAuthValidation AccessTokenFormat to use a MachineKey DataProtectionProvider and DataProtector which will protect and unprotect your bearer tokens.您可以将 OAuthValidation AccessTokenFormat 设置为使用 MachineKey DataProtectionProvider 和 DataProtector 来保护和取消保护您的不记名令牌。 You will need to implement the MachineKey DataProtector.您将需要实施 MachineKey DataProtector。 This guy already did it https://github.com/daixinkai/AspNetCore.Owin/blob/master/src/DataProtection/src/AspNetCore.DataProtection.MachineKey/MachineKeyDataProtectionProvider.cs .这家伙已经做到了https://github.com/daixinkai/AspNetCore.Owin/blob/master/src/DataProtection/src/AspNetCore.DataProtection.MachineKey/MachineKeyDataProtectionProvider.cs

public void ConfigureServices(IServiceCollection services){
   services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
   ConfigureAuth(services);

  string machineKey = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
        <machineKey decryption=""Auto"" decryptionKey =""DEC_KEY"" validation=""HMACSHA256"" validationKey=""VAL_KEY"" />";
        var machineKeyConfig = new XmlMachineKeyConfig(machineKey);
        MachineKeyDataProtectionOptions machinekeyOptions = new MachineKeyDataProtectionOptions();
        machinekeyOptions.MachineKey = new MachineKey(machineKeyConfig);
        MachineKeyDataProtectionProvider machineKeyDataProtectionProvider = new MachineKeyDataProtectionProvider(machinekeyOptions);
        MachineKeyDataProtector machineKeyDataProtector = new MachineKeyDataProtector(machinekeyOptions.MachineKey);

   //purposes from owin middleware
   IDataProtector dataProtector = 
   machineKeyDataProtector.CreateProtector("Microsoft.Owin.Security.OAuth",
               "Access_Token", "v1"); 

   services.AddAuthentication(options =>
   {
       options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
   })
   .AddOAuthValidation(option=> {
            option.AccessTokenFormat = new OwinTicketDataFormat(new OwinTicketSerializer(), dataProtector); })

It's important to keep the same DataProtector "purposes" Owin uses in the OAuthAuthorizationServerMiddleware so the data is encrypted/decrypted correctly.保持 Owin 在 OAuthAuthorizationServerMiddleware 中使用的相同 DataProtector “目的”很重要,以便正确加密/解密数据。 Those are "Microsoft.Owin.Security.OAuth", "Access_Token" and "v1".它们是“Microsoft.Owin.Security.OAuth”、“Access_Token”和“v1”。 (see https://stackoverflow.com/a/29454816/2734166 ). (参见https://stackoverflow.com/a/29454816/2734166 )。

And finally you will have to migrate the Owin TicketSerializer (and maybe the TicketFormat too) since the one in NetCore is slightly different.最后,您将不得不迁移 Owin TicketSerializer(也可能是 TicketFormat),因为 NetCore 中的略有不同。 You can grab it from here:你可以从这里获取它:

https://github.com/aspnet/AspNetKatana/blob/e2b18ec84ceab7ffa29d80d89429c9988ab40144/src/Microsoft.Owin.Security/DataHandler/Serializer/TicketSerializer.cs https://github.com/aspnet/AspNetKatana/blob/e2b18ec84ceab7ffa29d80d89429c9988ab40144/src/Microsoft.Owin.Security/DataHandler/Serializer/TicketSerializer.cs

I got this working recently.我最近得到了这个工作。 Basically authenticating to a .NET 4.5 Owin API and running a resource API in NET Core using the same token.基本上是对 .NET 4.5 Owin API 进行身份验证,并使用相同的令牌在 NET Core 中运行资源 API。 I'll try to share the code in github as soon as I clean it up.清理完我会尝试在github中共享代码。

As far as I know it's not recommended to keep the old machine key data protector, but to migrate to the new ones from NET Core.据我所知,不建议保留旧的机器密钥数据保护器,而是从 NET Core 迁移到新的。 Sometimes this is not possible.有时这是不可能的。 In my case I have too many APIs already in production, so I'm trying some new NET Core APIs to work with the legacy ones.就我而言,我已经在生产中使用了太多 API,因此我正在尝试使用一些新的 NET Core API 来处理旧的 API。

You should try this Owin.Token.AspNetCore nuget package instead.你应该试试这个Owin.Token.AspNetCore nuget 包。 By following the code example provided in the README file I'm able to decode legacy tokens using the machine keys on .NET Core 3.1.通过遵循自述文件中提供的代码示例,我能够使用 .NET Core 3.1 上的机器密钥解码旧令牌。 Note: there's also an option to specify encryption method and validation method if the defaults are not working for you.注意:如果默认设置不适合您,还有一个选项可以指定加密方法和验证方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM