0

We have build a ASP WEB api. Authentication is done by HMAC in the Authorization header. On server side we check the HMAC code and if its valid the request proceeds to the resource. Our project structure consist of multiple layers.

  1. -API-
  2. -BusinessLogic-

In the businessLogic we have an LINQ to SQL connection. This connectionstring is dynamic and is get in the API layer. What is the best way to pass the connectionstring to the businessLogic ? We came up with the idea of adding an extra header with the connectionstring in it. We then get the specific header by System.Web.HttpContext.Current.Request.headers. This works but is it safe ?

2
  • 1
    Are you telling that, you pass connection string from clients (api consumers)? Commented Apr 8, 2015 at 18:06
  • No the connectionstring is not send from the client but is get on the server. Then on serverside we'll add a new header to the current http request with the connection string to use in de businessLogic. Commented Apr 9, 2015 at 14:15

1 Answer 1

1

You should try to me more clear because your question doesn't entirely makes sense to me.

"This connectionstring is dynamic and is get in the API layer"

Do you mean you store it somewhere in your web.config / app.config of you Web API project?

" What is the best way to pass the connectionstring to the businessLogic ?"

Do you want to pass it from your Web API project to a class of your "businessLogic" project? I would advise you to get it from the appSettings of the business logic (will inherit the one of your Web Api Project).

ConfigurationManager.ConnectionStrings["Your_Connection_String_Name_Here"] 

I would abstract your app settings into a separate class (+ interface) to more easily mockable for unit testing.

e.g:

public interface IGetAppSettings { NameValueCollection GetAll { get; } string MyConnectionString { get; } } public class MyAppSettings : IGetAppSettings { public NameValueCollection GetAll { get { return ConfigurationManager.AppSettings; } } public string MyConnectionString { get { var value = ConfigurationManager.ConnectionStrings["MyConnectionString"]; return value.ToString(); } } } 
Sign up to request clarification or add additional context in comments.

1 Comment

In the web api we get the connectionstrings from a database. Then we need to pass the connectionstring to the business logic layer to connect to the right database with Linq to SQL. The main problem is that we are building an API for multiple databases that use the same api only with different connectionstrings to connect to the right database.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.