如何在我的 APIController 中读取登录的用户?

How can I read the logged user in my APIController?

在 WebAPI mvc 项目中如何读取我的 APIController 中的登录用户?

  1. 通过添加 withCredentioals:true

    从客户端发送凭据请求

    $.ajax({ url: ajaxURL,
    跨域:真, xhr 字段:{ withCredentials:真 }, 成功:函数(数据){ 警报(数据); }, });

  2. 从您的 WebAPI 服务器添加以下装饰以允许跨源请求并允许凭据: [EnableCors(来源:“http://YourClientServer:PORT”,headers:“*”, 方法:“*”,SupportsCredentials = true)]

SupportsCredentials 会将 header access-control-allow-credentials = true 添加到您的回复中

  1. 现在您可以使用 User.Identity.Name