Web Api Archives - Digital Tool Factory blog Web Api Archives - Digital Tool Factory blog

The Digital Tool Factory Blog

How to Get the ID of the logged in user in asp.net mvc web api apicontroller

After much googling, and evening Bing-ing revealed nothing I discovered that for some reason I had the line

config.SuppressDefaultHostAuthentication();

In my WebAPIConfig.cs file – I removed that and everything magically worked and I could use User.Identity.GetUserId();


28
Feb 22


Written By Steve French

 

How to do a cross domain json request with jquery and asp.net mvc web api

So – I was trying to request some data from one server from another – not normally a big deal, but the data would vary depending on whether or not the user was logged in or not.  I thought just setting up CORS would work (I’m using asp.net mvc web api 2).  I thought a simple jquery .post or .get would do the job, but surprisingly the .get and .post do not send the auth cookie when making the request – you have to use the .ajax features of jQuery, as well as enabling the “SupportsCredentials” part of Cors – the relevant parts look like this

In your web api controller

[EnableCors(origins: “*”, headers: “*”, methods: “*”,  SupportsCredentials = true)]

Your javascript code should look like this

$.ajax({
url: ‘http://MyWebServiceDomain.com/api/v.1.1/SomeController/MyFeature’,
dataType: ‘json’,
xhrFields: {
withCredentials: true
},
crossDomain: true,
success: function (authText) {
$(‘#authText’).html(authText);
}
});


19
Jan 17


Written By Steve French

 

How to fix 404 errors in asp.net web api 2

So – you’ve tried everything and you’re still getting weird 404 errors in asp.net web api 2.    You’ve tried the 4 main ways of fixing it, and you still get nothing – that was what I did anyway.  Finally I started a new project in the solution, and noticed that the problem only occured when other projects were referenced – and that I had several duplicate controller names.

I.E. Project A had and “AjaxController” and Project B had an “AjaxController”.  Everything works fine until you reference Project A from Project B – if you change the name of the controller in Project B the problem goes away and everything works perfectly.


26
Sep 16


Written By Steve French

 




Copyright 2011 Digital Tool Factory. All Rights Reserved. Powered by raw technical talent. And in this case, WordPress.