none
How to pass windows credential to ASP.NET Core Web API controller RRS feed

  • Question

  • I developed the ASP.NET Core Web API using windows autherntication. The URL is http://abc.com:8080. 

    I can login to this Web API via browser and get the data back within the company internal network. I enabled the windows autherntication in IIS site.

    Now I want to call this Web API from another ASP.net web site such as SharePoint 2019 with the same domain name as the Web API URL. It's https://abc.com:443. I used the same windows credential login to the SharePoint.

    I saw the error message that "The request needs credential / autherntication ...",  the api call is failure.

    How to pass the windows credential to  the ASP.NET Core Web API?

    • Moved by CoolDadTx Monday, July 8, 2019 2:05 PM ASP.NET related
    Saturday, July 6, 2019 5:27 AM

All replies

  • ASP.NET WebAPI can be discussed at the WebAPI forum in ASP.NET forums.

    http://forums.asp.net/

    Saturday, July 6, 2019 6:38 AM
  • Hi Linda,

    Thank you for posting here.

    According to your description, I suggest you could refer to below codes to know how to use httpclient send the request with windows credentials.

    More details, you could refer to below codes:

    using System;
    using System.Net;
    using System.Net.Http;
    
    namespace WinAuthAPICore
    {
        class Program
        {
            static void Main(string[] args)
            {
         
                var clienthandler = new HttpClientHandler() {
                    Credentials = new NetworkCredential("yourwindowsaccount", "password")
                };
    
                var client = new HttpClient(clienthandler);
                client.DefaultRequestHeaders.Accept.Clear();
              client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
                string result = client.GetStringAsync("https://localhost:44357/api/values").Result;
    
                int i = 0;
    
            }
        }
    }
    

    Core web api controller:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Authorization;
    using Microsoft.AspNetCore.Mvc;
    
    namespace CoreAPIServer.Controllers
    {
        [Authorize]
        [Route("api/[controller]")]
        [ApiController]
        public class ValuesController : ControllerBase
        {
            // GET api/values
            [HttpGet]
            public ActionResult<IEnumerable<string>> Get()
            {
                return new string[] { "value1", "value2" };
            }
    
            // GET api/values/5
            [HttpGet("{id}")]
            public ActionResult<string> Get(int id)
            {
                return "value";
            }
    
            // POST api/values
            [HttpPost]
            public void Post([FromBody] string value)
            {
                // For more information on protecting this API from Cross Site Request Forgery (CSRF) attacks, see https://go.microsoft.com/fwlink/?LinkID=717803
            }
    
            // PUT api/values/5
            [HttpPut("{id}")]
            public void Put(int id, [FromBody] string value)
            {
                // For more information on protecting this API from Cross Site Request Forgery (CSRF) attacks, see https://go.microsoft.com/fwlink/?LinkID=717803
            }
    
            // DELETE api/values/5
            [HttpDelete("{id}")]
            public void Delete(int id)
            {
                // For more information on protecting this API from Cross Site Request Forgery (CSRF) attacks, see https://go.microsoft.com/fwlink/?LinkID=717803
            }
        }
    }
    

    Result:

    Best Regards,

    Jack



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, July 8, 2019 7:55 AM