I'm trying to set up a WebAPI
with MVC5
/ Web Api 2
, it's currently using default configurations and other defaults right now. When I log in with an account in the browser, I can go to regular MVC
controller actions with the [Authorize]
attribute just fine, such as the Home page shows as it should when authorized, but then if I go to /api/Me
(a default built-in api controller action) or any custom api controller action I've built with the default MVC Web Api 2 scaffolding that requires authorization, I get an error like so:
{"message":"Authorization has been denied for this request."}
This is when I'm trying it in Microsoft Edge, I haven't tried it on my actual client code I'm building yet, which is a UWP
app. I figured I would test in the browser first to ensure things are working properly.
I started looking at this article: https://docs.microsoft.com/en-us/aspnet/web-api/overview/security/individual-accounts-in-web-api It seems to be more geared towards Ajax
requests and SPAs
though. My goal is having things working through both the web and through UWP
. I will probably focus more time on the UWP
side than developing a robust web app with ajax, since the app will run in an intranet and access an intranet IIS server running the web api, I'd like to build a desktop client in UWP and eventually Xamarin that access data through Web Api.
Is it safe for me to assume that if you're using a regular web browser like Edge you cannot access Web Api controller actions when they are secured through the [Authorize]
attribute, since it doesn't send an access token in the header?
So from your UWP app you could create a couple of methods such as below, or may be wrap them up in a class that you can inject through DI (your choice).
And if you are using basic (username and password authentication) then your GetHttpClient method will be something like:
or if you are using bearer token then you could do:
I'm posting a little more supplemental information here for anyone who might be struggling with this issue where you want to access MVC5 / Web Api 2 from a client like UWP or Xamarin, but you need to lock down areas of your Web Api with the Authorize attribute.
The high level process is, instead of going through any MVC controllers you're going to do a POST directly to the /Token endpoint (or whatever endpoint specified).
First thing's first, if you want to do SSL for development but don't have a cert yet, go ahead and run the following:
Where 55970 is the port of whatever your local website port is, I'll be referring to this in my text.
Next thing to do is be sure and override ApplicationOAuthProvider.cs on the client validation and resource granting methods, otherwise you'll run into invalid client or invalid grant errors.
You don't need to be as verbose as I was on the allowed clients, just "web" is fine, you'll include that in your Http POST method as a url-encoded form value, along with grant_type = "password" and your username/password. Here's a quick and dirty UWP client I wrote which just takes in a username/password and accesses a dataset I have via the api that's tied down with an Authorize attribute. If you don't authenticate you'll get an authorization error, which is exactly what we want.
Just a note here that in my client I filter out self-signed certificate errors because I've set up a self-signed cert in IIS Express via the above command.
Hope that helps any of you guys having trouble getting Xamarin or UWP working with Web Api.