c# - 跨两个 Web API 项目共享 OAuth token

标签 c# asp.net-web-api oauth

我已经创建了一个带有 OAuth token 身份验证的 Web API 应用程序。当 token 服务器与服务在同一应用程序上运行时,这没有问题。但是,我想将授权服务移动到它自己的应用程序(VS 项目)中,并在我正在处理的几个 Web API 项目中使用它。但是,当我将授权逻辑隔离到它自己的项目中时,原始服务不再将生成的 token 视为有效。我的问题是,一个 Web API 项目是否可以为另一个项目生成 token 以供验证?这是我的身份验证服务和原始服务的 OWIN 启动代码

授权服务:

public void Configuration(IAppBuilder app)
    {
        // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888
        HttpConfiguration config = new HttpConfiguration();

        ConfigureOAuth(app);

        WebApiConfig.Register(config);
        app.UseWebApi(config);
        app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
    }

    private void ConfigureOAuth(IAppBuilder app)
    {
        OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
        {
            AllowInsecureHttp = true,
            TokenEndpointPath = new PathString("/token"),
            AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
            Provider = new SimpleAuthorizationServerProvider()
        };

        // Token Generation
        app.UseOAuthAuthorizationServer(OAuthServerOptions);
        app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
    }

原始服务:

public void Configuration(IAppBuilder app)
    {
        ConfigureOAuth(app);
        // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888
        HttpConfiguration config = new HttpConfiguration();

        config.SuppressDefaultHostAuthentication();
        config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));


        WebApiConfig.Register(config);

        app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
        app.UseWebApi(config);
    }

    public void ConfigureOAuth(IAppBuilder app)
    {
        var oauthBearerOptions = new OAuthBearerAuthenticationOptions();
        app.UseOAuthBearerAuthentication(oauthBearerOptions);
    }

最佳答案

我自己在研究这个问题时偶然发现了这个问题。 TL;DR 的答案是 token 是使用 machine.config 文件中的 machineKey 属性生成的:如果你想在多台服务器上托管,你需要覆盖它。

可以在 web.config 中覆盖 MachineKey :

<system.web>
<machineKey validationKey="VALUE GOES HERE" 
            decryptionKey="VALUE GOES HERE" 
            validation="SHA1" 
            decryption="AES"/>
</system.web>

机器 key 应在本地生成 - 使用在线服务安全。 KB Article for generating keys

此处所有内容的原始引用 http://bitoftech.net/2014/09/24/decouple-owin-authorization-server-resource-server-oauth-2-0-web-api

关于c# - 跨两个 Web API 项目共享 OAuth token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25749818/

相关文章:

c# - 使用第三方 dll 的 VisualStudio NUnit3TestAdapter 测试项目

c# - WebApi 可以有一个端点并根据发送的对象做一些不同的事情吗

c# - 企业库记录 block 滚动平面文件不写入文件

asp.net - 在同一项目的 MVC 和 Web API 中使用 InstancePerHttpRequest 而不是 InstancePerApiRequest 有什么缺点?

OAuth 2.0 与 Google Analytics API v3

c# - 在毛伊岛构建节拍器,但声音播放器不规则并且对计时器不响应。间隔

c# - 如何使用 XAML/C# 创建透明的 'hotspot'

java - 使用 Google Apps Engine 端点的 OAuth 登录停止工作

authentication - PHP 网站中的 Facebook 集成

c# - ContinueWhenAll 是否观察到异常并因此防止 UnobservedTaskException?