2012-09-08 51 views
-1

如何替換和擴展此代碼以在認證憑證中使用密碼和用戶名。我試圖通過分析mscrm sdk中的示例來解決這個問題,但沒有運氣,因爲我不是c#程序員。如何使用用戶名和密碼連接到mscrm?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

using System.ServiceModel.Description; 
using Microsoft.Xrm.Sdk.Client; 
using System.Net; 
using Microsoft.Xrm.Sdk; 

public partial class _Default : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 

    } 

    protected void id_Click(object sender, EventArgs e) 
    { 

     AuthenticationCredentials authCredentials = new AuthenticationCredentials(); 

     //Authenticate using credentials of the logged in user;  
     ClientCredentials Credentials = new ClientCredentials(); 


     Uri OrganizationUri = new Uri("http://Crm/Contoso/XRMServices/2011/Organization.svc"); 
     Uri HomeRealmUri = null; 

     //OrganizationServiceProxy serviceProxy;  
     using (OrganizationServiceProxy serviceProxy = new OrganizationServiceProxy(OrganizationUri, HomeRealmUri, Credentials, null)) 
     { 
      IOrganizationService service = (IOrganizationService)serviceProxy; 

      //Instantiate the contact object and populate the attributes. 
      Entity contact = new Entity("contact"); 
      contact["firstname"] = txtFirstName.Text.ToString(); 
      contact["lastname"] = txtLastName.Text.ToString(); 
      Guid newContactId = service.Create(contact); 
     } 
    } 
} 

謝謝!

回答

1

我打算假設您在此處使用Active Directory身份驗證。 MSDN上有一個相當長的示例,它顯示瞭如何爲所有身份驗證方法創建連接。

我相信你只需要改變:

ClientCredentials Credentials = new ClientCredentials(); 

要:

ClientCredentials Credentials = new ClientCredentials(); 
Credentials.UserName.UserName = "domain\username"; 
Credentials.UserName.Password = "password"; 
0

我會建議使用默認憑據,如果可能的話,如果沒有可能那麼我們就用這樣的:

ClientCredentials creds = new ClientCredentials(); 
creds.Windows.ClientCredential = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["username"], ConfigurationManager.AppSettings["password"], ConfigurationManager.AppSettings["domain"]); 

該方法明顯的缺點是密碼明文,但上邊是你不需要重新編譯來更改密碼或用戶。

相關問題