Web API授权access_token验证

b_i*_*n_U 6 c# api asp.net-mvc access-token

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

https://localhost/Profile?access_token=...
Run Code Online (Sandbox Code Playgroud)

或者可以通过标头发送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;
    }
Run Code Online (Sandbox Code Playgroud)

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

jan*_*awa 8

您无需担心服务器端的访问令牌.服务器端的访问令牌由Katana中间件解析和验证.如果您需要有关如何创建/使用访问令牌的更多详细信息,然后在Katana源中搜索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();
    }
Run Code Online (Sandbox Code Playgroud)

如果你看一下Thinktecture的例子,将是最好的.