请参阅下面的客户端代码:
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
为false。为什么这是假的?
答案 0 :(得分:0)
从不在域中的计算机(例如工作组)连接时,您的调用者/ API无法使用Windows身份验证。
你必须改变你的来电者和API使用另一种身份验证方法。如果在IIS服务器上进行本地用户设置,则需要使用基本身份验证。