【问题标题】:How do you pass authentication information from one .net Web Api method to another如何将身份验证信息从一种 .net Web Api 方法传递到另一种
【发布时间】:2012-04-17 21:14:06
【问题描述】:

使用 .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 返回一个空对象、没有身份、未经过身份验证等:

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

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

【问题讨论】:

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


    【解决方案1】:

    您只需在 HttpClient 中包含来自请求的 cookie - 如果身份验证对两者都有效。这里解释一下setting the cookie in the HttpClient

    【讨论】:

    • 那里应该有链接吗?
    • 对不起!是的,忘记了。给你。
    • 没有真正的帮助。不过还是谢谢。
    • 没有真正的帮助。不过谢谢。我能够向客户端添加 cookie:new HttpClient().DefaultRequestHeaders.Add(...) 我可以在第二个 Api 方法中看到这些 cookie,但 GetUserPrincipal 仍然是空的。查看 dominick baier 的描述架构 (leastprivilege.com/…) 的帖子,但没有提供任何特定的解决方案,看起来原始调用上下文可能在第二个请求中丢失了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-14
    • 2013-01-29
    • 1970-01-01
    • 1970-01-01
    • 2014-02-24
    相关资源
    最近更新 更多