验证资源服务器中的Oauth Bearer令牌

时间:2016-05-04 10:25:33

标签: c# security oauth-2.0 owin

我正在尝试使用oauthAuthorizationServerProvider编写自己的oauth身份验证服务器。客户端向Authserver请求令牌。如果客户端凭据是有效的auth服务器,则提供访问令牌。现在,客户端将每个请求的令牌发送到资源服务器。我无法理解资源服务器将如何验证由auth服务器生成的令牌。任何人都可以使用oauthAuthorizationServerProvider提供任何示例代码。

以下是我尝试过的实现:

public class AuthorizationServerProvider : OAuthAuthorizationServerProvider
{
    public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
    {
        return Task.FromResult<object>(context.Validated());
    }

    public override Task TokenEndpoint(OAuthTokenEndpointContext context)
    {
        foreach (KeyValuePair<string, string> property in context.Properties.Dictionary)
        {
            context.AdditionalResponseParameters.Add(property.Key, property.Value);
        }
        return Task.FromResult<object>(null);
    }

    public override Task TokenEndpointResponse(OAuthTokenEndpointResponseContext context)
    {
        string path = @"e:\temp\MyTest.txt";
        File.WriteAllText(path, context.AccessToken);
        return base.TokenEndpointResponse(context);
    }
}


public void Configuration(IAppBuilder app)
    {
        ConfigureOAuth(app);
    }

    public void ConfigureOAuth(IAppBuilder app)
    {
        OAuthBearerOptions = new OAuthBearerAuthenticationOptions();

        OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions() { 
            AllowInsecureHttp=true,
            TokenEndpointPath= new PathString("/Token"),
            AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(20),
            Provider = new AuthorizationServerProvider(),

        };
        app.UseOAuthAuthorizationServer(OAuthServerOptions);
        app.UseOAuthBearerAuthentication(OAuthBearerOptions);
    }

现在我无法使用postman对其进行测试。如果我的实施对client_credentials授权是正确的,有人可以帮助我。

P.S:我想调试TokenEndpointResponse方法以及startup中的类。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

令牌包含授权服务器生成的身份验证票证。资源服务器从令牌中提取票证并检查它是否有效。

此任务由Microsoft.Owin.Security.OAuth dll完成。

授权服务器和资源服务器必须共享用于加密令牌内的身份验证票证的相同机器密钥,并解密令牌以获取票证。您可以将其包含在两个网站(服务器)的web.config中:

<system.web>
    ...
    <machineKey validationKey="BDE1234FBD71982481D87D815FA0A65B9F5982D123FA96E5672B78ABCD52D58818B479B19FF6D95263E85B0209297E68ABBA7D1E0BD3EABCD5E35742DEA5F2A7" 
        decryptionKey="8E8496D7342EA25ABCDEF6177E04EA00008E359C95E60CD0789456123B9ED2B3" 
        validation="SHA1" decryption="AES" />
    ...
</system.web>

TokenEndpointResponseOAuthAuthorizationServerProvider中执行的最后一个方法,只有当其他方法中的所有验证都正确时,才能在提供程序正常工作之前进行调试。

我通过post将我的oauth服务器实现基于以下Taiseer Joudeh,我认为你可以看到解释他的解释并查看代码。

我希望这会对你有所帮助。

相关问题