Reputation: 354
We are developing the windows forms application to authenticate a windows forms application to a Azure AD.
I have a couple of issues and the resources online are all seems to be outdated.
As suggested by the below link I have created a webapi and native client api and windows forms application.
In web api I have updated the manifest files as follows
"oauth2Permissions": [
{
"adminConsentDescription": "Allow the application to access ftpwebapi on behalf of the signed-in user.",
"adminConsentDisplayName": "Access ftpwebapi",
"id": "4eebeb18-aee6-408a-bea1-c090766dce23",
"isEnabled": true,
"origin": "Application",
"type": "User",
"userConsentDescription": "Allow the application to access ftpwebapi on your behalf.",
"userConsentDisplayName": "Access ftpwebapi",
"value": "user_impersonation"
}
],
And added under keys I have added an entry for 2 years and added the webapi to the native client application.
In the windows forms I have added the below code in the button click event.
private async void button1_Click(object sender, EventArgs e)
{
string authority = "https://login.windows.net/****";
string resourceURI = "https://***.com/WebApplication3";
string clientID = "********-5815-4358-****-*********";
Uri returnURI = new Uri("http://********.com");
AuthenticationContext authContext =
new AuthenticationContext(authority);
AuthenticationResult authResult =
authContext.AcquireToken(resourceURI, clientID, returnURI);
string authHeader = authResult.CreateAuthorizationHeader();
// don't do this in prod
System.Net.ServicePointManager.ServerCertificateValidationCallback =
((s, c, c2, se) => true);
HttpClient client = new HttpClient();
HttpRequestMessage request =
new HttpRequestMessage(HttpMethod.Get, "https://localhost:***/task/api/");
request.Headers.TryAddWithoutValidation("Authorization", authHeader);
var response = await client.SendAsync(request);
string responseString = await response.Content.ReadAsStringAsync();
MessageBox.Show(responseString);
}
Is it the right way so far ?
And I am agetting an error in
var response = await client.SendAsync(request);
An exception of type 'System.Net.Http.HttpRequestException' occurred in mscorlib.dll but was not handled in user code
Additional information: An error occurred while sending the request.
Upvotes: 2
Views: 36850
Reputation: 793
I run your code after setting these variables properly, at first I get the same error. Then I change my requesturl in httpRequestMessage request =new HttpRequestMessage(HttpMethod.Get, "https://localhost:***/task/api/")
.
Now it runs successfully, make sure your url https://localhost:***/task/api/
is right, double check your Port in localhost:***
.
Please read some tutorials to get more information from here.
Upvotes: 1