IsSuccessStatusCode is FALSE



请参阅下面的客户端代码:

namespace ReleaseLearning.Controllers
{
    public class CreateEmployeeController : Controller
    {
        //
        // GET: /CreateEmployee/
        HttpClientHandler handler = new HttpClientHandler()
        {
            UseDefaultCredentials = true
        };
        string uri = "http://localhost:52929/";

        public ActionResult CreateEmployee()
        {
            //http://localhost:52929/CreateEmployee/CreateEmployee
            //Employee Objemployee = new Employee();
            HttpClient client = new HttpClient(handler); //It will not authenticate if you remove handler (authorize attribute on server side)
            client.BaseAddress = new Uri(uri);
            // Add an Accept header for JSON format.
            client.DefaultRequestHeaders.Accept.Add(
            new MediaTypeWithQualityHeaderValue("application/json"));
            HttpResponseMessage response = client.GetAsync("api/Values").Result;
            if (response.IsSuccessStatusCode)
            {
                var EmployeeDetails = response.Content.ReadAsAsync<IEnumerable<Employee>>().Result;
            }
            //return View(objEmployee);
            return View();
        }
} }

以及下面的服务器端代码:

[HttpGet]

         [Authorize]
            public List<Employee> GetemployeeList()
            {
                string username = System.Environment.UserName;
                List<Employee> Employee = new List<Employee>{
    new Employee{Employee_ID=123,Employee_Name="Saurabh Sri"},
    new Employee{Employee_ID=1567,Employee_Name="Samir Saxena"},
    };
                return Employee;
            }

我在连接到域的PC上的Visual Studio中运行此代码,它按预期工作,即 response.IsSuccessStatusCode是真的。 但是,如果我在连接到工作组(而不是域(的PC上在Visual Studio中运行它,那么response.IsSuccessStatusCode是错误的。 为什么是假的?

从不在域(e.g.in 工作组(中的计算机进行连接时,调用方/API 无法使用 Windows 身份验证。

您必须更改调用方和 API 才能使用其他身份验证方法。 对于 IIS 服务器上的本地用户设置,则需要使用基本身份验证。

最新更新