简体   繁体   English

Web API授权access_token验证

[英]Web API authorization access_token validation

Now I am working with authorization with OAUTH2.0. 现在我正在使用OAUTH2.0授权。 I want to do my own authorization server(WEB API). 我想做自己的授权服务器(WEB API)。 I have a Dummy MVC project to test this. 我有一个Dummy MVC项目来测试这个。 I succeeded to create some access token in server(WEB API) using 'SimpleAuthorizationServerProvider'. 我成功地使用'SimpleAuthorizationServerProvider'在服务器(WEB API)中创建了一些访问令牌。 I have to call some API Calls but should authorized. 我必须调用一些API调用,但应该授权。 so I can send this call with my token like. 所以我可以用我的令牌发送这个电话。

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

or can send access_token through header. 或者可以通过标头发送access_token。 This much is OK now from my side. 从我这边现在可以了。 But I need to validate this access_token in server side. 但是我需要在服务器端验证这个access_token。 I can get access token from client(Dummy MVC project). 我可以从客户端获取访问令牌(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;
    }

But could not uderstand where it's created from server side. 但无法理解它是从服务器端创建的。 And Where we Can Validate the access_token in server side (Web API). 我们可以在哪里验证服务器端的access_token(Web API)。 I read lot but still very much confused. 我看了很多但仍然非常困惑。 Please help me. 请帮我。 Thanks!! 谢谢!!

You don't need to worry about access token on server side. 您无需担心服务器端的访问令牌。 Access token on server side is parsed and validated by Katana middleware. 服务器端的访问令牌由Katana中间件解析和验证。 If you need more details on how access token is created/used then search for DeserializeTicket and SerializeTicket methods in Katana sources , you will find that these methods are used in conjunction with Token to serialize/deserialize ClaimsIdentity which you have pased on client side(DummyMVC). 如果您需要有关如何创建/使用访问令牌的更多详细信息,然后在Katana源中搜索DeserializeTicket和SerializeTicket方法,您会发现这些方法与Token一起使用以序列化/反序列化您在客户端上设置的ClaimsIdentity(DummyMVC) )。

Anyway you are using SimpleAuthorizationServerProvider from Embedded AuthorizationServer Thinktecture project which is wrapper around OAuthAuthorizationServerProvider. 无论如何,您正在使用Embedded AuthorizationServer Thinktecture项目中的SimpleAuthorizationServerProvider,它是OAuthAuthorizationServerProvider的包装器。 Am I right? 我对吗? I belive you want to validate credentials. 我相信你想要验证凭据。 In your case you can override GrantResourceOwnerCredentials. 在您的情况下,您可以覆盖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();
    }

Best will be if you look at Thinktecture examples . 如果你看一下Thinktecture的例子将是最好的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Ocelot API网关中添加授权Header(授权:Bearer {access_token})到特定路由 - Adding Authorization Header (Authorization: Bearer {access_token}) to specific route in Ocelot API Gateway 如何在Web API C#中缓存授权访问令牌? - How to do caching of Authorization access token in web api C#? 使用Web API 2 C#处理通过请求发送的错误access_token - Handle bad access_token sent through request using Web API 2 C# Asp.net Web Api 2 - 如何从C#代码获取access_token - Asp.net Web Api 2 - how to get access_token from C# code 是否可以将Identity_4中的access_token从一个Web api中继到另一个Web api - Is it possible to relay access_token from one web api to another in IdentityServer4 基于令牌的授权Web API - Token Based Authorization Web API 为什么Facebook api返回#access_token与?access_token? - Why is the Facebook api returning #access_token vs ?access_token? Twitter api,无法通过 PKCE 的 2/oauth/token 获取 access_token - Twitter api, failed to get access_token by 2/oauth/token with PKCE 无法使用IdentityServer4验证API中的JWT或access_token - Unable to Validate JWT or access_token in API with IdentityServer4 尝试在ASP Web API和OWIN中使用Google auth_code获取access_token时获得的无效授权 - Invalid-grant when trying to get access_token with google auth_code in asp web api and OWIN
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM