All ClientID, ClientSecret & RedirectURI placed in the web.config
.
<appSettings>
<add key="redirectURI" value="http://localhost:55593/oauthplayground" />
<add key="clientId" value="uX4YpHHNm****ltekoG" />
<add key="clientSecret" value="K5cSv3izT1GZ9PXnaWWfRWbTv10*****O3JkYFMlWMF3FhBtjyk0FqJduGJZSAL7B1DngJyxgX3KKNSD0Bqdv" />
</appSettings>
Now from here I got the authentication code.
static string redirectURI = ConfigurationManager.AppSettings["redirectURI"];
static string clientID = ConfigurationManager.AppSettings["clientID"];
static string clientSecret = ConfigurationManager.AppSettings["clientSecret"];
protected void btnclick_Click(object sender, EventArgs e)
{
Response.Redirect(String.Format("https://d****o.com/o/authorize/?response_type=code&client_id={0}&redirect_uri={1}", clientID, redirectURI));
}
Then I got my authorization code:
3Q8tvb9d0fj232NZZIAIaItUIqtAd7
(I store this code in label i.e lblcode.Text
)
Then for Access_token & Refresh token, I use this code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.QueryString.Get("code") != null)
{ string AccessToken = string.Empty;
lblcode.Text = Request.QueryString["code"].ToString();
string RefreshToken = ExchangeAuthorizationCode(lblcode.Text, out AccessToken);
}
}
}
private string ExchangeAuthorizationCode(string code, out string accessToken)
{
accessToken = string.Empty;
string ClientSecret = clientSecret;
string ClientId = clientID;
//get this value by opening your web app in browser.
string RedirectUrl = redirectURI;
var Content = "code=" + code + "&client_id=" + ClientId + "&client_secret=" + ClientSecret + "&redirect_uri=" + RedirectUrl + "&grant_type=authorization_code";
var request = WebRequest.Create("https://drchrono.com/o/token/");
request.Method = "POST";
request.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequired;
byte[] byteArray = Encoding.UTF8.GetBytes(Content);
request.ContentType = "application/x-www-urlencoded";
request.ContentLength = byteArray.Length;
//ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
//System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
//ServicePointManager.ServerCertificateValidationCallback = (snder, cert, chain, error) => true;
using (Stream dataStream = request.GetRequestStream())
{
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
}
var Response = (HttpWebResponse)request.GetResponse();
Stream responseDataStream = Response.GetResponseStream();
StreamReader reader = new StreamReader(responseDataStream);
string ResponseData = reader.ReadToEnd();
reader.Close();
responseDataStream.Close();
Response.Close();
if (Response.StatusCode == HttpStatusCode.OK)
{
var ReturnedToken = JsonConvert.DeserializeObject<Token>(ResponseData);
if (ReturnedToken.refresh_token != null)
{
accessToken = ReturnedToken.access_token;
return ReturnedToken.refresh_token;
}
else
{
return null;
}
}
else
{
return string.Empty;
}
}
But I'm getting an error:
The request was aborted: Could not create SSL/TLS secure channel.
Note:
- Using POSTMAN, I get my all data. It means all API are working correctly.
- List item Comment in the Code, i used those but the same problems occurrs.
- I also checked my system with IISCrypto.exe in which i can see all my Server, Client Protocols(SSL 2.0, SSL 3.0, TLS 1.0, TLS 1. 1, TLS 1.2) are enabled.