Reputation: 303
My below code sample is working fine, but I would like to add my client credentials into the web.config file (i.e, Inside the connection string).
I tried but had no luck. Can anyone please help?
protected void Page_Load(object sender, EventArgs e)
{
// Organisation service URL
var organizationUri = new Uri(ConfigurationManager.ConnectionStrings["CrmConnectionStr"].ConnectionString);
//Client credentials
var credentials = new ClientCredentials();
credentials.UserName.UserName = @"domain\username";
credentials.UserName.Password = "password";
// Use the Microsoft Dynamics CRM Online connection string from the web.config file named "CrmConnectionStr".
using (OrganizationServiceProxy _service = new OrganizationServiceProxy(organizationUri, null, credentials, null))
{
Response.Write("Connected");
}
}
Web.config file
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="CrmConnectionStr" connectionString="https://test.domain.com/XRMServices/2011/Organization.svc" />
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
</configuration>
Upvotes: 0
Views: 2386
Reputation: 27852
Write a custom configuration to store your specific data (and do not try to piggy back on the existing connection strings area, which has an exact format).
Here is an example: How to include simple collections in ConfigurationSection
This is much better than having several disjointed AppSetting(s) values. Everything germane to your values is encapsulated into a known area.
Upvotes: 0
Reputation: 8147
As RandomWebGuy pointed out you could be connecting to Microsoft Dynamics CRM in which case you could just change your connection string to include the username etc like this connectionString="https://test.domain.com/XRMServices/2011/Organization.svc; Username=Fred.Bloggs; Password=P@ssword;"
However, if you connecting to a webservice or want to store arbitrary values such as URIs, usernames, passwords etc then use the AppSettings section, not ConnectionString.
So, something like this in your config file:
<appSettings>
<add key="UserName" value="Fred.Bloggs" />
<add key="Password" value="P@ssword" />
<add key="ServiceUri" value="https://test.domain.com/XRMServices/2011/Organization.svc />
</appSettings>
And then in code:
var organizationUri = new Uri(ConfigurationManager.AppSettings["ServiceUri"]);
// ...
credentials.UserName.UserName = ConfigurationManager.AppSettings["UserName"];
credentials.UserName.Password = ConfigurationManager.AppSettings["Password"];
Upvotes: 2