如何将身份验证信息从一个.net Web Api方法传递给另一个

时间:2012-04-17 21:14:06

标签: .net asp.net-web-api

使用.net Web Api和Forms身份验证,我有一个Api方法进行第二次Api调用。第一个Api方法是从javascript调用的,我可以使用GetUserPrincipal方法获取当前登录用户的身份:

private void Put(int id, object value) {
    var principal = ControllerContext.Request.GetUserPrincipal();
    ....
    var responseMessage = new HttpClient().PutAsync("/api/secondapicontroller/", null).Result
    ....
}

但是,当该方法进行链式内部Api调用时,GetUserPrincipal返回一个空对象,没有Identity,未经过身份验证等。

private void Put(int id, object value) {
    var principal = ControllerContext.Request.GetUserPrincipal();
    ....
}

如何将身份验证信息从第一个Api方法传递到第二个。

1 个答案:

答案 0 :(得分:0)

如果身份验证适用于两者,您只需要在HttpClient中包含请求中的cookie。这里解释一下setting the cookie in the HttpClient