0

I've built an intranet hosted MVC/EF/C# application that uses windows authentication and identity impersonation and connects to SQL server using integrated security. The EF connection string looks like:

<add name="BgEntities" connectionString="metadata=res://*/Models.Bg.csdl|res://*/Models.Bg.ssdl|res://*/Models.Bg.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=xxxxxxxx;initial catalog=Bg_Dev;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

This is great for regular app users. I now need to open a controller up to all users on the intranet, but I can't add everyone in the company as users to the database. I'd like to use a generic SQL Server login to access the database from this controller. Can I switch connection strings in this controller?

PeterH
  • 1
  • 2
  • Did you check this article before. https://stackoverflow.com/questions/23979124/mvc5-simple-membership-default-membership/23979181#23979181 – Prime Aug 14 '18 at 16:40

1 Answers1

0

So what worked for me was setting the connection string on the database context per Rodion's post here Configure multiple database Entity Framework 6.

I'm sure there's better ways, and i assume that this first connects to the db using the connection string specified in web.config first, then reconnects using the second connection string. But it gets the result.

    private bg db = new bg();
    public ActionResult Index(int? id)
    {
        db.Database.Connection.ConnectionString = "data source=xxx;initial catalog=bg;MultipleActiveResultSets=True;User Id=xxx;Password=yyy;App=EntityFramework";

;

PeterH
  • 1
  • 2