Search code examples
c#model-view-controllercross-domain-policy

How can I read the logged user in my APIController?


in a WebAPI mvc project How can I read the logged user in my APIController?


Solution

    1. send the credentials with the request from the client side by adding withCredentioals:true

      $.ajax({ url: ajaxURL,
      crossDomain: true, xhrFields: { withCredentials: true }, success: function (data) { alert(data); }, });

    2. From your WebAPI server Add the following decoration to allow the cross origin request and to allow the credentioals: [EnableCors(origins: "http://YourClientServer:PORT", headers: "*", methods: "*", SupportsCredentials = true)]

    SupportsCredentials will add the header access-control-allow-credentials = true to your response

    1. Now you can use the User.Identity.Name