我正在努力理解和设置一个服务和消费者,当用户登录到消费者时,服务将在其中运行。
我的消费者是一个 MVC 应用程序。我的服务是一个 Web Api 应用程序。两者都在同一域中的不同服务器上运行。两者都设置为使用 Windows 身份验证。
我的消费者代码是:
private T GenericGet<T>(string p)
{
T result = default(T);
HttpClientHandler handler = new HttpClientHandler() { PreAuthenticate = true, UseDefaultCredentials = true };
using (HttpClient client = new HttpClient(handler))
{
client.BaseAddress = new Uri(serviceEndPoint);
HttpResponseMessage response = client.GetAsync(p).Result;
if (response.IsSuccessStatusCode)
result = response.Content.ReadAsAsync<T>().Result;
}
return result;
}
在我的服务中,我调用User.Identity.Name
以获取呼叫者 ID,但这总是作为消费者应用程序池 ID,而不是登录用户返回。消费者应用程序池作为网络服务运行,服务器本身受委托进行委托。那么如何获取登录用户呢?服务代码:
// GET: /Modules/5/Permissions/
[Authorize]
public ModulePermissionsDTO Get(int ModuleID)
{
Module module= moduleRepository.Find(ModuleID);
if (module== null)
throw new HttpResponseException(HttpStatusCode.NotFound);
// This just shows as the App Pool the MVC consumer is running as (Network Service).
IPrincipal loggedInUser = User;
// Do I need to do something with this instead?
string authHeader = HttpContext.Current.Request.Headers["Authorization"];
ModulePermissionsDTO dto = new ModulePermissionsDTO();
// Construct object here based on User...
return dto;
}
根据这个问题,需要 Kerberos 才能进行此设置,因为 HttpClient 在单独的线程中运行。然而,这让我感到困惑,因为我认为请求发送了一个 Authorization 标头,因此服务应该能够使用它并检索用户令牌。无论如何,我已经使用 Kerberos 进行了一些测试,以使用此处“情况 5”中的演示检查这在我的域上是否正确工作,并且这工作但我的两个应用程序仍然无法正确传递登录用户。
那么我需要做什么才能完成这项工作?是否需要 Kerberos 或者我是否需要在我的服务中做一些事情来解压 Authorization 标头并从令牌创建一个主体对象?所有建议表示赞赏。