Reputation: 11
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.
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 ?
Upvotes: 0
Views: 786
Reputation: 8048
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();
}
}
}
Upvotes: 1