Error - connectionStringName attribute is not allowed - c#

I am using basic authentication for my web api and am getting the following error
*{"Message":"An error has occurred.","ExceptionMessage":"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL *
It works absolutely fine when i remove the basic auth implementation from my APIs. So am guessing the problem should be something related to system.web.providers which am using for the basic auth implementation.
This is how my connection string looks like
<connectionStrings>
<add name="DefaultConnection" connectionString="Password=password;Persist Security Info=True;User ID=wbuser;Initial Catalog=WinBouts_com;Data Source=WINBOUTSAPIVM"
providerName="System.Data.SqlClient" />
<add name="WinBoutsConnectionString" connectionString="Password=password;Persist Security Info=True;User ID=wbuser;Initial Catalog=WinBouts_com;Data Source=WINBOUTSAPIVM"
providerName="System.Data.SqlClient" />
And i see an error in my web.config where a session provider is added. The error says "the connectionStringName attribute is not allowed". But if i remove it then it asks for the connectionStringName. This is the code for it.
<!--
If you are deploying to a cloud environment that has multiple web server instances,
you should change session state mode from "InProc" to "Custom". In addition,
change the connection string named "DefaultConnection" to connect to an instance
of SQL Server (including SQL Azure and SQL Compact) instead of to SQL Server Express.
-->
<sessionState mode="Custom" customProvider="DefaultSessionProvider">
<providers>
<add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="WinBoutsConnectionString" />
</providers>
</sessionState>
Can anyone please tell me where i might be going wrong? Any suggestions or ideas would be deeply appreciated.

Finally Resolved the issue. The problem was in my Basic Authentication filter class.
private bool TryGetPrincipal(string username, string password, out IPrincipal principal)
{ string connectionString = ConfigurationManager.ConnectionStrings["WinBoutsConnectionString"].ConnectionString;
username = username.Trim();
password = password.Trim();
//only supporting SSO login atm
// var valid = Membership.Provider.ValidateUser(username, password);
serviceContext = new WinBoutsDataContext<DataAccess.Model.UserInfo>(connectionString);
userRepository = new UserRepository(serviceContext);
var valid = this.userRepository.ValidateAccount(username, password);
if (valid)
{
// once the user is verified, assign it to an IPrincipal with the identity name and applicable roles
principal = new GenericPrincipal(new GenericIdentity(username), System.Web.Security.Roles.GetRolesForUser(username));
return true;
}
//user wasn't found, unauthorised
principal = null;
return false;
}
I have hardcoded my DataContext here but earlier i didnt pass my connection string into it. I just went for the default constructor of the WinBoutsDataContext which didnt work when i put the code on to the server.

If you want to use a defined connectionString, you have to use the name of the connection string to fill the "sqlConnectionString" attribute and use SqlServer mode :
<configuration>
<connectionStrings>
<add name = "myString"
connectionString = "data source=local;user id=User;password=password" />
</connectionStrings>
<system.web>
<sessionState
mode="SQLServer"
sqlConnectionString="myString"
cookieless="false"
timeout="20"
/>
</system.web>
</configuration>

Related

How can I make connection string for LDAP by two attributes DC=intechww,DC=com

I am pretty much new to the LDAP connection to a remote server Active Directory. My system administrator has provided me just two things. “DC=intechww,DC=com” I have network administrator username and password and so far I have tried many combination but not getting my response from the network.
I need to validate user credentials if user exist in Active directory then get his username and other details and store in my ASP.NET MVC app database.
My connection string for LDAP is:
<connectionStrings>
<add name="ADConnectionString"
connectionString="LDAP://intechww.com/DC=users,DC=com" />
</connectionStrings>
Other Web.config details are
<system.web>
<authentication mode="Forms">
<forms name=".ADAuthCookie" loginUrl="~/User/Verify" timeout="15" slidingExpiration="false" protection="All" />
</authentication>
<membership defaultProvider="MY_ADMembershipProvider">
<providers>
<clear />
<add name="MY_ADMembershipProvider"
type="System.Web.Security.ActiveDirectoryMembershipProvider"
connectionStringName="ADConnectionString"
attributeMapUsername="sAMAccountName"
connectionUsername="ServiceAccount"
connectionPassword="********* " />
</providers>
</membership>
</system.web>
My C# controller method is:
[HttpPost]
public ActionResult Verify(User model)
{
if (Membership.ValidateUser(model.username, model.password))
{
FormsAuthentication.SetAuthCookie(model.username, true);
}
else
{
ModelState.AddModelError("", "The user name or password provided is incorrect");
}
return View();
}
The DC= parts are the domain components that make up your LDAP name.
So given the inputs you've received, I'd try the following connection strings:
LDAP://intechww.com/DC=intechww,DC=com
This would connect to the root of the domain.
Or:
LDAP://intechww.com/CN=Users,DC=intechww,DC=com
This would connect to the default Users container of the domain.
Check out the Sysinternal AD Explorer - an invaluable tool to take a peek inside your AD and to understand the various bits and pieces in there (and how they are addressed by LDAP URLs).

Connection to SQL server database in ASP

I know my question is basic.
But I don't Know how should I configure web.config information!
My SQL server is connected with windows authentication.
I'm using this config:
<add key="DBServer" value="mssql" />
<add key="DBSource" value="localhost" />
<add key="DBName" value="TestDB" />
<add key="LoginName" value="KARIM-PC\Karim" />
<add key="LoginPWD" value="" />
but I get this error:
{"This operation requires a connection to the 'master' database. Unable to create a connection to the 'master' database because the original database connection has been opened and credentials have been removed from the connection string. Supply an unopened connection."}
Exception: Thrown: "Login failed for user 'KARIM-PC\Karim'."
(System.Data.SqlClient.SqlException) A
System.Data.SqlClient.SqlException was thrown: "Login failed for user
'KARIM-PC\Karim'."
Add either <add key="TrustedConnection" value="Yes" /> or <add key="IntegratedSecurity" value="SSPI" />to force Windows Authentication.
You can read more here.
For Window Authentication
<add name="MyDbConn" connectionString="Initial Catalog=MyDataBaseNAme;Data Source=MyServerName;Integrated Security=SSPI;"/>
I recommend you this site: https://www.connectionstrings.com/sql-server/ for getting more information about connection strings.
In your case I think it will suits you:
Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;

SQL Server Connection String: Web Hosting Service providing connection string that doesn't seem right

I have an ASP.NET C# application with a SQL server 2008 R2 database that has consolidated membership provider and application database. I am trying to modify my web config according to the instructions provided by the web hosting service provider but I think I'm missing something.
Here is my current web.config connection strings:
<connectionStrings>
<add name="RestaurantDB" connectionString="data source=SFP\SFP_SQL_SVR;attachdbfilename=C:\Users\Susan\Documents\Databases\Restaurants.mdf;Integrated Security=SSPI;"/>
<add name="RestaurantsEntities" connectionString="metadata=res://*/Restaurant.csdl|res://*/Restaurant.ssdl|res://*/Restaurant.msl;provider=System.Data.SqlClient;provider connection string="data source=SFP\SFP_SQL_SVR;attachdbfilename=C:\Users\Susan\Documents\Databases\Restaurants.mdf;integrated security=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
Here is the connection string that the web hosting service has given me:
Data Source=RESTAURANT.db.9319451.hostedresource.com; Initial Catalog=RESTAURANT; User ID=your username; Password='your password';
How do I modify my current connection strings in light of the above? Thanks in advance.
Just replace data source=SFP\SFP_SQL_SVR;attachdbfilename=C:\Users\Susan\Documents\Databases\Restaurants.mdf;Integrated Security=SSPI;
to
Data Source=RESTAURANT.db.9319451.hostedresource.com; Initial Catalog=RESTAURANT; User ID=your username; Password='your password';
Like :
<add name="RestaurantDB" connectionString="data source=RESTAURANT.db.9319451.hostedresource.com;
Initial Catalog=RESTAURANT; User ID=your username; Password='your password'>
<add name="RestaurantsEntities" connectionString="metadata=res://*/Restaurant.csdl|res://*/Restaurant.ssdl|res://*/Restaurant.msl;
provider=System.Data.SqlClient;provider connection string="Data Source=RESTAURANT.db.9319451.hostedresource.com;
Initial Catalog=RESTAURANT; User ID=your username; Password='your password;
multipleactiveresultsets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
I have no idea how to deal with this but I'm just giving you an idea. Try it may be it works
First modify your RestaurantDB connection string like this
<add name="RestaurantDB" connectionString="RESTAURANT.db.9319451.hostedresource.com;Initial Catalog=RESTAURANT; User ID=your username; Password='your password';"/>
and RestaurantsEntities like this
<add name="RestaurantsEntities" connectionString="metadata=res://*/Restaurant.csdl|res://*/Restaurant.ssdl|res://*/Restaurant.msl;provider=System.Data.SqlClient;provider connection string="data source=RESTAURANT.db.9319451.hostedresource.com;Initial Catalog=RESTAURANT;integrated security=True;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
If the above does not work then also add UserID and Password in the RestaurantsEntities connection string

How to connect database by using username and password in .net c#

I'm new to .net c#, hope this question is not sound silly. How can I connect to the database in web.config by using the username and password??
Example:
Following is the connection strings that I used to connect to the database. How could I write/set so that I can set the username (username = test) and password (password = abc123) for this connection strings and so that it will allows me to access to the database?
<connectionStrings>
<add name="aConnectionString" connectionString="Data Source=AAA-LLLL-SQL-00;Initial Catalog=Database_Name;Connect Timeout=1;Integrated Security=True;" providerName="System.Data.SqlClient"/>
</connectionStrings>
Try something like this.
<connectionStrings>
<add name="aConnectionString" connectionString="Data
Source=AAA-LLLL-SQL-00;Initial Catalog=Database_Name;User Id=myUsername;Password=myPassword"
providerName="System.Data.SqlClient"/>
</connectionStrings>
Use this to pull your connection string from the web.config.
http://msdn.microsoft.com/en-us/library/ms178411.aspx
Then you will use the .net Sql Provider to create a connection.
using(SqlConnection con = new SqlConnection(connectionstring))
{
con.Open();
// Perform operations here
}
Are you trying from IIS?
Then you need to grant your App Pools account access to SQL Server.
CREATE LOGIN [IIS APPPOOL\ASP.NET v4.0] FROM WINDOWS WITH DEFAULT_DATABASE=[AAA-LLLL-SQL-00], DEFAULT_LANGUAGE=[us_english]
GO
Reference for ADO and ADO.NET connection strings http://www.sqlstrings.com/
<connectionStrings>
<add name="aConnectionString" connectionString="Server={ip};Database= {databasename}; User Id={username}; Password={password}"/>
</connectionStrings>
This should work replace {...} with your values.
<connectionStrings>
<add name="aConnectionString" connectionString="Data Source=AAA-LLLL-SQL-00;Initial Catalog=Database_Name; User Id=your username; Password=pwd;" providerName="System.Data.SqlClient"/>
</connectionStrings>

sql 2008 support for SqlCacheDependency?

according to :
http://msdn.microsoft.com/en-us/library/system.web.caching.sqlcachedependency.aspx
if im using sql 2005 i have to write this :
<configuration>
<connectionStrings>
<add name="Northwind" connectionString="Data Source=(local); Initial Catalog=northwind; Integrated Security=true"; providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<caching>
<sqlCacheDependency enabled = "true" pollTime = "60000" >
<databases>
<add name="northwind"
connectionStringName="Northwind"
pollTime="9000000"
/>
</databases>
</sqlCacheDependency>
</caching>
</system.web>
</configuration>
but what about sql 2008?
i'm enabling the service broker ,
i'm creating sqlCacheDpendency and put there the Command
and when i change the Table , it doesn't remove the cached item.
Read The Mysterious Notification to understand how it works and then you can understand how to troubleshoot it (or cheat out with ssbdiagnose). Most likely the EXECUTE AS context of the database was invalidated by orphaning the dbo due to database movement. The transmission_status value in sys.transmission_queue would clearly show it. If true, the fix is to map dbo to a valid login:
ALTER AUTHORIZATION ON DATABASE::[<dbname>] TO [sa];
Otherwise the solution depends o the problem cause, use the troubleshooting steps linked above.

Categories

Resources