Web API授权access_token验证

时间:2014-05-01 07:03:11

标签: c# asp.net-mvc api access-token

现在我正在使用OAUTH2.0进行授权。 我想做自己的授权服务器(WEB API)。 我有一个Dummy MVC项目来测试这个。 我成功地使用'SimpleAuthorizationServerProvider'在服务器(WEB API)中创建了一些访问令牌。 我必须调用一些API调用,但应该授权。 所以我可以用我的令牌发送这个电话。

https://localhost/Profile?access_token=...

或者可以通过标头发送access_token。 从我这边现在可以了。 但是我需要在服务器端验证这个access_token。 我可以从客户端获取访问令牌(Dummy MVC项目)。

private static TokenResponse GetToken()
    {
            var client = new OAuth2Client(new Uri("http://localhost:2727/token"),"client1", "secret");
            var response = client.RequestResourceOwnerPasswordAsync("bob", "bob").Result;
            return response;
    }

但无法理解它是从服务器端创建的。 我们可以在哪里验证服务器端的access_token(Web API)。 我看了很多但仍然非常困惑。 请帮我。 谢谢!

1 个答案:

答案 0 :(得分:8)

您无需担心服务器端的访问令牌。服务器端的访问令牌由Katana中间件解析和验证。如果您需要有关如何创建/使用访问令牌的更多详细信息,然后在Katana sources中搜索DeserializeTicket和SerializeTicket方法,您会发现这些方法与Token一起使用来序列化/反序列化您在客户端上使用的ClaimsIdentity侧(DummyMVC)。

无论如何,您正在使用Embedded AuthorizationServer Thinktecture项目中的SimpleAuthorizationServerProvider,它是OAuthAuthorizationServerProvider的包装。我对吗?我相信你想要验证凭据。在您的情况下,您可以覆盖GrantResourceOwnerCredentials。

    public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
    {
        // validate user credentials (demo!)
        // user credentials should be stored securely (salted, iterated, hashed yada)
        if (context.UserName != context.Password)
        {
            context.Rejected();
            return;
        }
        context.Validated();
    }

如果你看Thinktecture examples,那将是最好的。

相关问题