Specifiy Connection String When Publishing Web - visual-studio-2012

I have a web site I am building where I need to deploy to two environments: stage and prod.
I have three connection strings specified in my web.config.
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=MyWebAppWaitWebDb;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\MyWebAppWaitWeb.mdf" providerName="System.Data.SqlClient" />
<add name="MyWebAppConnection_Stage" connectionString="Server=StageSQL01T;Database=MyWebAppWaitWeb;User Id=foo;Password=bar" providerName="System.Data.SqlClient" />
<add name="MyWebAppConnection_Prod" connectionString="Server=ProdSQL01;Failover Partner=ProdSQL02;Database=prd_SP_ExternalData;User Id=foo;Password=bar" providerName="System.Data.SqlClient" />
In the Publishing Web Wizard I am presented all three of these connection strings in the "settings" portion of the wizard. So in this case I chose stage and un-checked the others so that I can publish to my stage server.
Upon publishing I find it added a new connection called "MyWebAppConnection_Stage_DatabasePublish"
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=MyWebAppWaitWebDb;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\MyWebAppWaitWeb.mdf" providerName="System.Data.MyWebApp" />
<add name="MyWebAppConnection_Stage" connectionString="Data Source=StageSQL01T;Initial Catalog=MyWebAppWaitWeb;User ID=foo;Password=bar" providerName="System.Data.MyWebApp" />
<add name="MyWebAppConnection_Prod" connectionString="Server=ProdSQL01;Failover Partner=ProdSQL02;Database=prd_SP_ExternalData;User Id=MyWebAppWebApp;Password=bar" providerName="System.Data.MyWebApp" />
<add name="MyWebAppConnection_Stage_DatabasePublish" connectionString="Data Source=StageSQL01T;Initial Catalog=MyWebAppWaitWeb;User ID=foo;Password=bar" providerName="System.Data.MyWebApp" />
Why is this 4th connection string being generated? Furthermore, how is this VS 2010 Publishing compatible with the DbContext class I have for my Entity Code-first class that specifies the connection string?
public class MyContextDb : DbContext
{
public MyContextDb(): base("DefaultConnection")
{
Debug.Write(Database.Connection.ConnectionString);
}
...
I would think that I would want to use traditional web.config transformation which simply alters one connection string according to the transform file being used.

Related

What is the proper format to substitute ConnectionString variable in release pipeline?

I am using following connection string in my web.config file
<configuration>
<connectionString>
<add name="DefaultConnection" connectionString="__DefaultConnection__" providerName="System.Data.SqlClient" />
</connectionString>
<appSettings>
<add key="webpages:Version" value="3.0.0.0" />
<add key="webpages:Enabled" value="false" />
<add key="ClientValidationEnabled" value="__ClientValidationEnabled__" />
<add key="UnobtrusiveJavaScriptEnabled" value="true" />
</appSettings>
And in IIS Web Deploy release pipeline I am using XML Variable Substitution, by defining a variable with the key name DefaultConnection and value as Server=127.0.0.1;Database=myDataBase;Trusted_Connection=True;
The variable in appsettings gets changed on the server-side but not for the connectionString, Is it the correct way of declaring connection string. I am doing this for only test purposes, and do not have valid connections setup. Also in Microsoft documentation here, the format is quite different. Can anyone help me, in this case, how can I use this only for test purposes, keeping in mind the IIS server doesn't go down.
As defined in the Microsoft documentation, the connection string will have to be of the same format, as you have used in the variable value like this
Server=127.0.0.1;Database=myDataBase;Trusted_Connection=True
Make these changes in your config file:
<connectionString>
<add name="DefaultConnection" connectionString="Server=local;Database=undefined;Trusted_Connection=False
" providerName="System.Data.SqlClient" />
</connectionString>
Now run your pipelines and you should be able to see changes in web.config on server side.

How to change database from Local to Host to use Identity model

I have a databse that located to App_Data folder with below connection string:
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-mydb-20171102055914.mdf;Initial Catalog=aspnet-mydb-20171102055914;Integrated Security=True"
providerName="System.Data.SqlClient" />
I've used it for Asp Identity User Manager Model.
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
but I want to use the database that is located in server. my server connection string structure is something like :
<add name="DefaultConnection" connectionString="metadata=res://*/Models.Database.mydbModel.csdl|res://*/Models.Database.mydbModel.ssdl|res://*/Models.Database.mydbModel.msl;provider=System.Data.SqlClient;provider connection string='data source=134.54.96.45,1355;initial catalog=Db_Server;user id="serverUser";password=serverPass;MultipleActiveResultSets=True;App=EntityFramework'" providerName="System.Data.EntityClient" />
the server connection string works correctly for other models. but when I want to use ASP Identity Model, its caused error. How I cant fix it?
I fixed this by creating a standard connection string that pointed to the database where the ASP.NET Identity tables are.
<add name="DefaultConnection" connectionString="Data Source=134.54.96.45,1355; Initial Catalog=Db_Server; User ID="serverUser"; Password=serverPass; Connect Timeout=60;" providerName="System.Data.SqlClient" />
I do not know why the Identity Model just worked with this connection string and what is the diffrent between these connction strings.
but this last works correctly for me.

Deploying a WCF Data Service/REST Service on IIS 7.5 localhost

Here are the details of my problem. I have one simple WCF Data Service (named WCFServiceAppCBS.svc) using an Entity Framework data access layer that talks to a SqlServer 2008 R2 datasource to return some entities. I just want to expose a few tables as "GET" to be later consumed by external getJSON/AJAX calls in some html files.
For development purposes, it works fine when I'm playing around with it in VS2010 using IIS Express and can consume the OData URI and return data. But, the OData service doesn't return anything when I deploy it to the localhost IIS 7.
All I get is the Atom Pub feed that lists my entities, but when I try to execute any type of iQueryable statements (i.e. http://localhost/WCFServiceAppCBS/OData.svc/officers), I get a generic "the website cannot display the page".
I'm not sure if it is having a problem authenticating or if there are other settings in my Web.Config or IIS that I'm missing.
Here's my web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0">
<assemblies>
<add assembly="System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</assemblies>
</compilation>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true" />
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
<connectionStrings>
<add name="CBSEntities" connectionString="metadata=res://*/CBSLookup.csdl|res://*/CBSLookup.ssdl|res://*/CBSLookup.msl;provider=System.Data.SqlClient;provider connection string="data source=QCSQL2K8DEV;initial catalog=CBS;integrated security=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
Here's my OData.svc.cs...
using System;
using System.Collections.Generic;
using System.Data.Services;
using System.Data.Services.Common;
using System.Linq;
using System.ServiceModel.Web;
using System.Web;
namespace WCFServiceAppCBS
{
public class OData : DataService<CBSEntities>
{
// This method is called only once to initialize service-wide policies.
public static void InitializeService(DataServiceConfiguration config)
{
// TODO: set rules to indicate which entity sets and service operations are visible, updatable, etc.
// Examples:
config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
// config.SetServiceOperationAccessRule("MyServiceOperation", ServiceOperationRights.All);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V2;
}
}
}
Thanks for any help you can provide!

How do I access the currently logged in user from new controllers I create?

Here is my problem. I'm building a web application using MVC3 framework. My application started as the sample website included in the MVC3 Installation.
That web application was already setup for users to be able to login.
My problem is this.
I need to save files when the user logs in, I need the files to be created under folders named after the UserName
How do I access the Logged in users info and profile? When I evaluate HttpContext.Current.User It returns my windows NTLM logon name, not the name i logged into the site with. Here is what my web.config looks like.
<profile defaultProvider="AspNetSqlProfileProvider" enabled="true">
<providers>
<clear/>
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />
</providers>
<properties>
<add name ="TaxPayerID" type="String"/>
</properties>
</profile>
<roleManager enabled="true">
<providers>
<clear/>
<add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" />
<add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
</providers>
</roleManager>
Your custom controller should inherit from the base Controller class. Access the logged in user via:
User.Identity.Name
I had removed the authentication section of my web.config file. I realized that right after I posted this

Azure Membership UI

Using AspProviders (TableStorageMembershipProvider etc) from Microsoft WCF Azure Samples.
It is WCF Service Web Role, and in Azure Storage Explorer I can see Membership, Roles and Session tables appearing nicely when I try to connect.
But is there any exisiting code to manage Membership and Roles?
Some ASPX pages I guess, something like this for plain old ASP.NET, but more modern and Azure-tested hopefully?
Thanks,
Andres
To my surprise, Peter Kellner's solution I mentioned works just fine. Here are the steps to follow: Add AspProviders (from WCF Azure Samples) to your solution. Create Azure ASP.NET Web Role (let's call it MembershipAdmin), reference AspProviders from it. Add Membership.aspx and other aspx/cs files from Peter Kellner's solution, mark Membership.aspx as "Set As Start Page". In MembershipAdmin's Web.config, find and replace sections "membership" and "roleManager", and add section "appSettings" (the latter goes directly under root configuration). Examples of these sections are below, just change "YourTargetService" to what your real service name is. Actually it's going to mirror Web.config of your real service, regarding these sections (well, you need a few more tricks there, too - as a matter of completeness I'd like to mention I got help from here and here). Then just run, and appearing Membership.aspx is quite self explaining. Of course securing that page will be obviously your next worry, but at least now roles/users can be managed, and they appear nicely into Azure Table Storage.
<membership defaultProvider="TableStorageMembershipProvider" userIsOnlineTimeWindow="20">
<providers>
<clear />
<add name="TableStorageMembershipProvider" type="Microsoft.Samples.ServiceHosting.AspProviders.TableStorageMembershipProvider" description="Membership provider using table storage" applicationName="YourTargetService" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" minRequiredPasswordLength="1" minRequiredNonalphanumericCharacters="0" requiresUniqueEmail="true" passwordFormat="Hashed" />
</providers>
</membership>
<roleManager enabled="true" defaultProvider="TableStorageRoleProvider" cacheRolesInCookie="true" cookieName=".ASPXROLES" cookieTimeout="30" cookiePath="/" cookieRequireSSL="false" cookieSlidingExpiration="true" cookieProtection="All">
<providers>
<clear />
<add name="TableStorageRoleProvider" type="Microsoft.Samples.ServiceHosting.AspProviders.TableStorageRoleProvider" description="Role provider using table storage" applicationName="YourTargetService" />
</providers>
</roleManager>
<appSettings>
<add key="TableStorageEndpoint" value="http://127.0.0.1:10002/devstoreaccount1" />
<add key="BlobStorageEndpoint" value="http://127.0.0.1:10000/devstoreaccount1" />
<add key="AccountName" value="devstoreaccount1" />
<add key="AccountSharedKey" value="Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==" />
<add key="DefaultMembershipTableName" value="Membership" />
<add key="DefaultRoleTableName" value="Roles" />
<add key="DefaultSessionTableName" value="Sessions" />
<add key="DefaultProviderApplicationName" value="YourTargetService" />
<add key="DefaultProfileContainerName" />
<add key="DefaultSessionContainerName" />
</appSettings>

Resources