I need OAuth2 implementation in C# Windows Form application.
I have
ClientID
as "
abc
" and
client secret as "
xyz
".
Further, Auth Url = "https://login.windows.net/{TenentID}/oauth2/authorize?resource=https://api.businesscentral.dynamics.com",
Redirect URI = "https://businesscentral.dynamics.com/",
Access Token URL = "https://login.windows.net/TenentID/oauth2/token?resource=https://api.businesscentral.dynamics.com".
My use case is as follows:
I have a Windows Form native application. We are using Microsoft dynamics application for the finance department. A Windows native app has been used for the core business. My requirement is to integrate two applications to communicate with each other via web API. From MS Dynamics, they have given OAuth2 authorization to access their API endpoints.
I created an access token generating application using C#, but the generated token doesn't allow me to update the resource and said that Unauthorize.
I think I have done something wrong with the implementation. I tried to get the Auth code, but all the efforts fail. I need help to solve the issue.
Further, I will explain how my Auth application works according to the below code sample.
Step 1 - Once the program loads, it asks me to enter a valid Microsoft Email Id and password.
Step 2 - After login process succeeds, it redirect me to Dynamics Login Page.
Step 3 - I can enter DY user credentials as well and log in to the application as well.
Step 4 - Meanwhile, I tried to trigger Authorization code to pass to the method Call (
GetAccessTokenAsync
). It returned
null
.
What I have tried:
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Identity.Client;
using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.WinForms;
using Microsoft.Web.WebView2.Wpf;
using System;
using System.Security.AccessControl;
using System.Security.Policy;
using System.Web;
using System.Windows.Forms;
namespace OAuthAuth
{
public partial class Form1 : Form
{
private const string clientId = "ABC";
private const string clientSecret = "XYZ";
private const string AuthUrl =
"https://login.windows.net/{TenentID}/oauth2/authorize";
private const string redirectUri =
"https://businesscentral.dynamics.com/";
readonly string[] Scope = { ".default " };
private string authorizationCode;
public Form1()
{
InitializeComponent();
webBrowser.NavigationCompleted += WebView_NavigationCompleted;
webBrowser.Source = new Uri(AuthUrl);
}
private void button1_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
var tokenParams = new Dictionary<string, string>
{
{ "grant_type", "authorization_code" },
{ "client_id", clientId },
{ "client_secret", clientSecret },
{ "code", authorizationCode },
{ "redirect_uri", redirectUri }
};
}
private void Form1_Load(object sender, EventArgs e)
{
string authUri = $"{AuthUrl}?response_type=code&
client_id={clientId}&redirect_uri={redirectUri}&
resource=https://api.businesscentral.dynamics.com";
webBrowser.Source = new Uri(authUri);
}
private void WebView_NavigationCompleted
(object sender, CoreWebView2NavigationCompletedEventArgs e)
{
string currentUrl = webBrowser.Source.AbsoluteUri;
Uri uri = new Uri(currentUrl);
if (currentUrl.StartsWith(redirectUri))
{
string code = QueryHelpers.ParseNullableQuery(uri.Query).
GetValueOrDefault("code");
string authorizationCode = currentUrl.Split('?')
[1].Split('&').FirstOrDefault(q =>
q.StartsWith("code="))?.Replace("code=", "");
}
}
}
}