Monday 15 May 2023

Azure B2B Token Auth | Microsoft azure

 Azure B2B Token auth once of best way for auth.

Way to Generate Auth Token:

public async Task<DataResponse> GenerateB2BToken()

        {

            List<KeyValuePair<string, string>> postData = new List<KeyValuePair<string, string>>

                    {

                        new KeyValuePair<string, string>(CLIENT_CREDENTIALS),

                        new KeyValuePair<string, string>(B2CClientId),

                        new KeyValuePair<string, string>(B2CClientSecret),

                        new KeyValuePair<string, string>(B2BResource)

                    };

            using (var client = new HttpClient())

            {

                _httpClient.DefaultRequestHeaders.Accept.Clear();

                _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

                FormUrlEncodedContent content = new FormUrlEncodedContent((List<KeyValuePair<string, string>>)postData);

                using HttpResponseMessage response = await client.PostAsync(urlPath, content);

                {

                    var responsedata = await response.Content.ReadAsStringAsync();                    

                    return responsedata;

                }

            }

            if (!string.IsNullOrEmpty(response))

            {

                TokenAuthData token = JsonConvert.DeserializeObject<TokenAuthData>(response);

                if (token != null && !string.IsNullOrWhiteSpace(token.token_type) && !string.IsNullOrWhiteSpace(token.access_token))

                {                     

                       return Json( new { Token = $"{token.token_type} {token.access_token}" });                   

                }

                

            }

            return  null;

        }

 Token Validation : 

public async Task<bool> ValidateB2BToken(string idtoken)

        {

            string _token = idtoken.Replace(BEARER, "").Replace(BEARER.ToUpper(), "").Trim();

            if (!string.IsNullOrEmpty(_token))

            {

                try

                {

                    var token = new JwtSecurityToken(jwtEncodedString: _token);

                    if (token.Claims != null && token.Claims.Count() > 0)

                    {

                        var expiry = long.Parse(token.Claims.Where(x => x.Type == "exp").Select(x => x.Value).FirstOrDefault());

                        var aud = token.Audiences.Where(x => x == "your client id").Select(x => x).FirstOrDefault();

                        long epochTime = await ToUnixTime(DateTime.UtcNow);

                         //validate expiry and audience

                        if (!string.IsNullOrEmpty(aud) && epochTime <= expiry)

                        {

                            return true;

                        }

                        else

                            return false;

                    }

                }

                catch (Exception ex)//Token expired case

                {

                    

                    return false;

                }

            }

            return false;

        }

        private async Task<long> ToUnixTime(DateTime date)

        {

            var epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

            return await Task.FromResult( Convert.ToInt64((date - epoch).TotalSeconds));

        }


Above way you can validate Token auth B2b

Thursday 16 February 2023

Git fails when pushing commit to github | try increasing http.postBuffer

 Recently I faced issue while pushing more files to GIT, here is solution if you face similar error message which title of this blog represent,

unable to rewind rpc post data - try increasing http.postBuffer

Error: RPC failed; curl 56 OpenSSL SSL_read: Connection was reset, errno 10054

send-pack: unexpected disconnect while reading sideband packet


Solution:

Open command prompt and copy paste below line  hit on enter,

git config http.postBuffer 524288000
Above statement will increase http write buffer size, huge number of file you can able to push then.


Tuesday 7 February 2023

How to truncate a foreign key constrained table?

 Recent time I've faced challenge while mysql table truncation data,

Cannot truncate a table referenced in a foreign key constraint 

In above case error, 

MySQL Query: 

  SET FOREIGN_KEY_CHECKS = 0;

  truncate table mydb.TableName

  SET FOREIGN_KEY_CHECKS = 1;

You could able to do truncate entire table which having foreign key as well.