How to configure Web Api 2 and IIS for basic authentication?

Scenario

  • I have an Api 2.2 RESTful service made with VS2012 C #
  • I use basic authentication with user user / password verification (works fine)
  • Some methods have an attribute [AllowAnonymous]
  • Several methods use [Authorize(Roles = "myRol")]
  • In local works (read the user and go through, check it using the database, deny or allow, when necessary, allow anonymity, when necessary, etc.)

Problem

When I first opened the service in the DEV environment, I was unable to log in:

  • Ways with AllowAnonymous return 200
  • Basic Security Methods 401

Data (please request or comment on this update)

This is my web.config:

 <system.webServer> <modules> <add name="BasicAuthHttpModule" type="WebHostBasicAuth.Modules.BasicAuthHttpModule, myAssemblyName" /> </modules> 

This is my header (it works in local mode):

 Authorization: Basic YXR1cG9uZTp0ZXN0 Content-Type: application/json; charset=utf-8 

and this is my answer:

Status 401 Unauthorized
Data : {"message":"Authorization has been denied for this request."}
Title :

 Pragma: no-cache Date: Fri, 24 Apr 2015 14:36:27 GMT Server: Microsoft-IIS/8.0 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Content-Type: application/json; charset=utf-8 Cache-Control: no-cache Content-Length: 61 Expires: -1 

This is my IIS configuration (if I set to " on " Basic Authentication, IIS will ask me to provide credentials that I don’t have and / or they are invalid):

IIS Config


Question:

Is it well configured? Did I miss something?
How can I see a log or something about what is the internal error of this?

Thanks in advance.

+5
source share
1 answer

It looks like your authorization code cannot connect to the database.

Comment on the code that connects to the database, and return true regardless of whether it allows you to pass. Use an exception to narrow down to the point of the problem. You need to update the DB connection strings (web.config), etc., so that they match the dev environment. You cannot expect to work on a local computer to work in a different environment if you have not configured it accordingly. Hope this helps.

0
source

All Articles