1

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.

screenshot of sample request

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.

cookie sample ss

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?

dglumesh
  • 101
  • 1
  • 4
  • 15

1 Answers1

3

Here's a fun life hack when using Postman:

enter image description here

enter image description here

EDIT:

I'm not sure if I understand the question.

If you have the value, just put it in the header

request.AddHeader("Cookie",value)

If you want to define your .net Core app to allow the use of Session Cookies you can try going to Startup.cs and in the ConfigureServices method add:

services.AddSession(options =>
            {
                options.IdleTimeout = TimeSpan.FromSeconds(int.Parse(_configuration["Cookies:ExpiresInSecond"]));
                options.Cookie.Name = _configuration["Cookies:CookiesName"];
                options.Cookie.IsEssential = true;
                options.Cookie.SameSite = SameSiteMode.Unspecified;
            });

the _configuration is taken from the appSettings.json file in your .NET Core project.

EDIT 2:

If you wish to set a value to your session Cookie, use:

_httpContext.HttpContext.Session.Set(KEY, Encoding.ASCII.GetBytes(VALUE));

If you wish to get a value from your session Cookie:

_httpContext.HttpContext.Session.TryGetValue(KEY, out byte[] VALUE_BYTES);
var value = VALUE_BYTES == null ? "" : Encoding.ASCII.GetString(VALUE_BYTES);

If you wish to add the value you just pulled from Session Cookie to your request header use:

request.AddHeader("Cookie",value)

Hope that covers everything :)

Yoni Ziv
  • 166
  • 1
  • 10
  • Thank you for your answer. This is cool. I didn't aware about that. Can I know how to put session cookie value inside request.AddHeader("Cookie","???") – dglumesh Aug 24 '20 at 09:51
  • This is my first time using api request with cookie header. I added the that cookie configuration to my startup class. One last question how do I pass the cookie value after that to request.AddHeader("Cookie",???). – dglumesh Aug 24 '20 at 10:30
  • Try running it with the configuration first. I think it should be enough If you wish to add a specific Coockie to your header, well it's just as you wrote: request.AddHeader("Coockie",[your value]) – Yoni Ziv Aug 24 '20 at 10:43
  • Thank you Yoni Ziv. I will try this out – dglumesh Aug 24 '20 at 11:00
  • Any idea how to get token auth cookies from startup.cs? Do i need to do extra steps other than above startup.cs file code? I updated the question. – dglumesh Aug 24 '20 at 12:52
  • Edited answer. Hope this answers the question – Yoni Ziv Aug 24 '20 at 13:59
  • Its covers everything. Thanks again Yoni :) – dglumesh Aug 24 '20 at 14:56
  • if you can please help on this https://stackoverflow.com/questions/63575769/how-to-get-tokenauthcookies-cookie-from-docs-microsoft-com-in-asp-net-core-m Much appreciate :) – dglumesh Aug 26 '20 at 03:11