I need to get token array from https://token.learn.microsoft.com/accesstokens. This can be done by include cookies to the http request header. I already tested with postman. It works.
I have web application with ASP .Net core Mvc. It will authenticate with Azure AD. After user log in i need to send a API request include with 'Cookie' request header.
static async Task<string> SendURI(Uri u, HttpContent c, string token)
{
var response = string.Empty;
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", token);
HttpRequestMessage request = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = u,
Content = c
};
HttpResponseMessage result = await client.SendAsync(request);
if (result.IsSuccessStatusCode)
{
response = result.StatusCode.ToString();
}
}
return response;
}
This is how I send the request. Its working fine for other request without cookie request header. But I need to know how to add cookies to this http request from my app. https://learn.microsoft.com/en-us/rest/api/resources/tenants/list#code-try-0 in this doc they using this method. I need the same effort with my app. They send post request to https://token.learn.microsoft.com/accesstokens this with cookie header.
UPDATE: adding header cookie to http request is can be done by help of post man code.
Now I face a challenge with creating custom cookie like ms doc using.
How can I create customer cookie inside startup.cs file? for the content value what should to be included?
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromSeconds(20);
options.Cookie.Name = ".TokenAuthCookies";
options.Cookie.Path = "/";
options.Cookie.Domain = ".learn.microsoft.com";
options.Cookie.IsEssential = true;
options.Cookie.SameSite = SameSiteMode.Unspecified;
});
How I change my above code accordingly to achieve this?