Hi
My code is working if I pass grant_type="client_credentials" Body in POSTMAN. But If i pass grant_type in params. It give me "error": "unsupported_grant_type".
Please see in below screenshot.

How can i take grant_type from url query string.
See below my code is working with grant_type passed in Body.
public class AppAuthorizationServerProvider : OAuthAuthorizationServerProvider
{
string _contextClientId = "";
string _contextCleintSecret = "";
public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
{
string clientId = string.Empty;
string clientSecret = string.Empty;
_contextClientId = context.Parameters.Get("clientId");
_contextCleintSecret = context.Parameters.Get("clientSecret");
if (context.TryGetBasicCredentials(out clientId, out clientSecret))
{
context.SetError("invalid _client", "client credential could not be retrived");
context.Rejected();
return Task.CompletedTask;
}
ClientDetails client = (new ClientDetailsRepo()).ValidateClient(_contextClientId, _contextCleintSecret);
if (client != null)
{
context.OwinContext.Set
context.Validated();
}
else
{
context.SetError("invalid _client", "client credentials are not valid");
context.Rejected();
}
//context.Validated();
return Task.CompletedTask;
}
public override async Task GrantClientCredentials(OAuthGrantClientCredentialsContext context)
{
//Guid clientId;
//Guid.TryParse(context.ClientId, out clientId);
//validate aginstdb or config: GetByClientId(clientId);
//string clientId = context.ClientId;
bool client = ConfigurationManager.AppSettings["ClientId"] == _contextClientId && ConfigurationManager.AppSettings["ClientSecret"] == _contextCleintSecret;
if (!client)
{
context.SetError("invalid_grant", "Invaild client.");
context.Rejected();
return;
}
var claimsIdentity = new ClaimsIdentity(context.Options.AuthenticationType);
claimsIdentity.AddClaim(new Claim("LoggedOn", DateTime.Now.ToString()));
claimsIdentity.AddClaim(new Claim("ClientId", _contextClientId));
claimsIdentity.AddClaim(new Claim("ClientSecret", _contextCleintSecret));
await Task.Run(() => context.Validated(claimsIdentity));
}
}
Manish VadukulPosted Jan 10, 2024, 4:44 PM
Hi Dhiraj,
Please help
I have tried your code. It validate client. Which is fine. Same as before.
But didn't trigger (Hit) this function. The below function heat only when if i use grant_type under Body in Postman than it works. If it is URL than didn't trigger. and throw "error": "unsupported_grant_type" in POSTMAN
public override async Task GrantClientCredentials(OAuthGrantClientCredentialsContext context)
{
//Guid clientId;
//Guid.TryParse(context.ClientId, out clientId);
//validate aginstdb or config: GetByClientId(clientId);
//string clientId = context.ClientId;
bool client = ConfigurationManager.AppSettings["ClientId"] == _contextClientId && ConfigurationManager.AppSettings["ClientSecret"] == _contextCleintSecret;
if (!client)
{
context.SetError("invalid_grant", "Invaild client.");
context.Rejected();
return;
}
var claimsIdentity = new ClaimsIdentity(context.Options.AuthenticationType);
claimsIdentity.AddClaim(new Claim("LoggedOn", DateTime.Now.ToString()));
claimsIdentity.AddClaim(new Claim("ClientId", _contextClientId));
claimsIdentity.AddClaim(new Claim("ClientSecret", _contextCleintSecret));
await Task.Run(() => context.Validated(claimsIdentity));
}
Dhiraj PoojaryPosted Jan 10, 2024, 3:48 PM
It looks like your current code expects the
grant_typeparameter to be passed in the request body, and it doesn't handle the case where it's passed as a query parameter in the URL.To support both methods (either in the request body or as a query parameter), you need to modify the
ValidateClientAuthenticationmethod to check both locations for thegrant_type. Here's an updated version of your code:This modification checks both the request parameters and the query parameters for
clientIdandclientSecret. If they are not found in the request parameters, it checks the query parameters. If neither is found, it returns an "invalid_client" error. Adjust the code according to your specific requirements and security considerations. This code is not tested.