消费OWIN从asp.net核心发布的oAuth承载

问题描述 投票:1回答:1

我有带有OWIN管道的ASP.NET 4.5服务,它发出OAuth access_token以换取用户名/密码。此服务从ng-app调用,一旦获得令牌,就会存储在浏览器的本地存储中。然后它调用消耗此令牌的资源api,API也用asp.net 4.5编写并使用owin。这是与OWIN一起发布的OAuth令牌,它被加密/签名到machineKey机密 - 到目前为止一直很好,并且被资源API愉快地使用。所有这些都可以通过OAuthAuthorizationServerMiddleware实现。

现在我需要使用从同一个ng-app发送到asp.net核心2.1服务的这些令牌,没有什么花哨的,只需验证/解码它并获得令牌内的声明。 这个OAuthAuthorizationServerMiddleware从未移植到asp.net核心,所以我被卡住了。 (OAuth Authorization Service in ASP.NET Core没有帮助它谈论完全成熟的oidc服务器,我只需要消耗它们而无需更改发布代码)在ConfigureServices()中加入了service.AddAuthentication():Tried.AddJwtBearer - 但这没有任何意义 - 这些不是Jwt令牌真的是Tried.AddOAuth但这没有意义b / c我没有用重定向处理完整的OAuth流来获取令牌,我也没有处理ClientId / ClientSecret / etc,我只是在ng app的HTTP头中接收“Bearer token-here”,所以我需要管道中的东西来解码它并设置ClaimsIdentity但是这个“管道中的东西”也需要访问类似机器的数据,如同它是在asp.net 4.5 OWIN服务有什么想法吗?

c# asp.net-core owin
1个回答
4
投票

您可以将OAuthValidation AccessTokenFormat设置为使用MachineKey DataProtectionProvider和DataProtector,它将保护和取消保护您的承载令牌。您需要实现MachineKey DataProtector。这家伙已经做到了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); })

保持Owin在OAuthAuthorizationServerMiddleware中使用的相同DataProtector“目的”非常重要,这样才能正确加密/解密数据。这些是“Microsoft.Owin.Security.OAuth”,“Access_Token”和“v1”。 (见https://stackoverflow.com/a/29454816/2734166)。

最后你将不得不迁移Owin TicketSerializer(也可能是TicketFormat),因为NetCore中的那个略有不同。你可以从这里抓住它:

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

我最近有这个工作。基本上对.NET 4.5 Owin API进行身份验证,并使用相同的令牌在.NET Core中运行资源API。一旦我清理它,我会尝试在github中共享代码。

据我所知,不建议保留旧的机器密钥数据保护器,而是从.NET Core迁移到新的机器密钥数据保护器。有时这是不可能的。在我的情况下,我已经有太多的API在生产中,所以我正在尝试一些新的.NET Core API来处理遗留的API。

© www.soinside.com 2019 - 2024. All rights reserved.