我的下面的代码示例工作正常,但我想将我的客户端凭据添加到web.config文件中(即,在连接字符串内)。
我试过但没有运气。有人可以帮忙吗?
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文件
<?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>
答案 0 :(得分:2)
由于RandomWebGuy指出您可以连接到Microsoft Dynamics CRM,在这种情况下您只需更改连接字符串以包含用户名等,如connectionString="https://test.domain.com/XRMServices/2011/Organization.svc; Username=Fred.Bloggs; Password=P@ssword;"
但是,如果您连接到网络服务或想要存储任意值,例如URI,用户名,密码等,请使用 AppSettings 部分,而不是 ConnectionString 。
所以,在你的配置文件中是这样的:
<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>
然后在代码中:
var organizationUri = new Uri(ConfigurationManager.AppSettings["ServiceUri"]);
// ...
credentials.UserName.UserName = ConfigurationManager.AppSettings["UserName"];
credentials.UserName.Password = ConfigurationManager.AppSettings["Password"];
答案 1 :(得分:0)
编写自定义配置来存储您的特定数据(不要试图捎带现有的连接字符串区域,它具有确切的格式)。
这是一个例子: How to include simple collections in ConfigurationSection
这比拥有多个脱节的AppSetting(s)值要好得多。 与你的价值观密切相关的一切都被封装在一个已知的领域。