OAuth中client id的处理

摘要:
http://www.tugberkugurlu.com/archive/simple-oauth-server-implementing-a-simple-oauth-server-with-katana-oauth-authorization-server-components-part-1https://docs.microsoft.com/en-us/以前的版本/a
http://www.tugberkugurlu.com/archive/simple-oauth-server-implementing-a-simple-oauth-server-with-katana-oauth-authorization-server-components-part-1

https://docs.microsoft.com/en-us/previous-versions/aspnet/mt180817(v%3Dvs.113)

Called to validate that the origin of the request is a registered "client_id", and that the correct credentials for that client are present on the request. If the web application accepts Basic authentication credentials, context.TryGetBasicCredentials(out clientId, out clientSecret) may be called to acquire those values if present in the request header. If the web application accepts "client_id" and "client_secret" as form encoded POST parameters, context.TryGetFormCredentials(out clientId, out clientSecret) may be called to acquire those values if present in the request body. If context.Validated is not called the request will not proceed further.

public override async Task ValidateClientAuthentication(
        OAuthValidateClientAuthenticationContext context)
    {
        string clientId;
        string clientSecret;

        if (context.TryGetBasicCredentials(out clientId, out clientSecret))
        {
            UserManager<IdentityUser> userManager = 
                context.OwinContext.GetUserManager<UserManager<IdentityUser>>();
            OAuthDbContext dbContext = 
                context.OwinContext.Get<OAuthDbContext>();

            try
            {
                Client client = await dbContext
                    .Clients
                    .FirstOrDefaultAsync(clientEntity => clientEntity.Id == clientId);

                if (client != null &&
                    userManager.PasswordHasher.VerifyHashedPassword(
                        client.ClientSecretHash, clientSecret) == PasswordVerificationResult.Success)
                {
                    // Client has been verified.
                    context.OwinContext.Set<Client>("oauth:client", client);
                    context.Validated(clientId);
                }
                else
                {
                    // Client could not be validated.
                    context.SetError("invalid_client", "Client credentials are invalid.");
                    context.Rejected();
                }
            }
            catch
            {
                // Could not get the client through the IClientManager implementation.
                context.SetError("server_error");
                context.Rejected();
            }
        }
        else
        {
            // The client credentials could not be retrieved.
            context.SetError(
                "invalid_client", 
                "Client credentials could not be retrieved through the Authorization header.");

            context.Rejected();
        }
    }
https://stackoverflow.com/questions/37119386/client-id-is-always-null-in-validateclientauthentication

if your client_id is passed as a form param, you'll have to get it by doing context.TryGetFormCredentials(out clientId, out clientSecret);

if your client_id is passed as an Authorization header, you can get it by doing context.TryGetBasicCredentials(out clientId, out clientSecret);

once you've got the client_id from your request, do context.Validated(clientId), this will set your context.ClientId property, this property will always be null until you've done context.Validated()

参考

https://www.cnblogs.com/amywechat/p/5591902.html

免责声明:文章转载自《OAuth中client id的处理》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇【转】 MATLAB界面程序实例学习SILK编码语音转WAV格式下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

WebAPI常见的鉴权方法,及其适用范围

在谈这个问题之前,我们先来说说在WebAPI中保障接口请求合法性的常见办法: API Key + API Secret cookie-session认证 OAuth JWT  当然还有很多其它的,比如 openid connect (OAuth 2.0协议之上的简单身份层),Basic Auth ,Digest Auth 不一一例举了 1、API Ke...

oauth2中client_id_to_access数据膨胀问题

  这是删除后,过了一两天就增长到了4万多条数据了。 查看了RedisTokenStore 发现token 会不断地往list塞值。 public void storeAccessToken(OAuth2AccessToken token, OAuth2Authentication authentication) { byte[] serializedAc...

OAuth2.0协议专区-Springcloud集成springsecurity oauth2实现服务统一认证,应该时最简单的教程了~

1.配置认证服务器(1) 首先配置springsecurity,其实他底层是很多filter组成,顺序是请求先到他这里进行校验,然后在到oauth /** * @author: gaoyang * @Description: 身份认证拦截 */ @Order(1) @Configuration //注解权限拦截 @EnableGloba...

SpringBootSecurity学习(17)前后端分离版之 OAuth2.0 数据库(JDBC)存储客户端

自动批准授权码 前面我们授权的流程中,第一步获取授权码的时候,都会经历一个授权是否同意页面: 这个流程就像第三方登录成功后,提问是否允许获取昵称和头像信息的页面一样,这个过程其实是可以自动同意的,需要在客户端配置中,增加一个自动批准: 这样我们申请授权码直接就可以得到: 在流程需要自动完成的时候,需要这样配置,如果需要用户点击同意,那么这里需要设置为...

WebApi使用Token(OAUTH 2.0方式)

1.在项目中添加引用 Microsoft.AspNet.WebApi.Owin Microsoft.Owin.Host.SystemWeb Microsoft.Owin.Security.OAuth Microsoft.Owin.Security.Cookies Microsoft.AspNet.Identity.Owin Microsoft.Owin.C...

Spring Cloud微服务安全实战_6-3_jwt认证之网关和服务改造

上一节在认证服务器里,将token 由uuid改造成了JWT,之前在网关上拿到令牌access_token后,需要去认证服务器校验令牌,将令牌信息转换为用户信息。 现在有了jwt后,由于jwt是自包含的,已经包含了用户的身份信息,所以在网关上不需要去认证服务器验令牌了。 之前在网关上所做的这些去认证服务器验令牌信息,转换为用户信息,去认证服务器做权限的判...