访问Dynamics365时出现错误“Microsoft.Xrm.Sdk.dll中发生System.ServiceModel.Security.MessageSecurityException”

时间:2018-05-31 06:02:11

标签: c# dynamics-crm

我正在尝试从MS Dynamics获取数据,但是出现错误  “'System.ServiceModel.Security.MessageSecurityException'发生在Microsoft.Xrm.Sdk.dll中,但未在用户代码中处理

附加信息:从另一方收到了无担保或不正确安全的故障。请参阅内部FaultException以获取故障代码和详细信息。“ 产生的代码如下

protected void Page_Load(object sender, EventArgs e)
    {
        IOrganizationService service = GetCRMService();
        QueryExpression query = new QueryExpression("account");
        query.ColumnSet.AllColumns = true;
        query.Criteria.AddCondition("name", ConditionOperator.NotEqual, "ksllls");
        EntityCollection collection = service.RetrieveMultiple(query);

        DataTable dt = new DataTable();
        dt.Columns.Add("name");
        dt.Columns.Add("accountnumber");
        foreach (Entity entity in collection.Entities)
        {
            DataRow dr = dt.NewRow();
            dr["name"] = entity.Attributes["name"].ToString();
            if (entity.Contains("accountnumber"))
            {
                dr["accountnumber"] = entity.Attributes["accountnumber"].ToString();
            }
            dt.Rows.Add(dr);
        }
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }


    public IOrganizationService GetCRMService()
    {

        string UserName = "mydomain.com";
        string Password = "*****";
        ClientCredentials Credentials = new ClientCredentials();
        IOrganizationService Service;
        Credentials.UserName.UserName = UserName;
        Credentials.UserName.Password = Password;

        Credentials.Windows.ClientCredential = CredentialCache.DefaultNetworkCredentials;
        //This URI need to be updated to match the servername and organisation for the environment

        string CRMServer = ConfigurationManager.AppSettings["crmserverurl"].ToString();
        Uri OrganizationUri = new Uri(CRMServer);
        Uri HomeRealmUri = null;
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
        // OrgaizationServiceProxy  ServiceProxy

        using (OrganizationServiceProxy ServiceProxy = new OrganizationServiceProxy(OrganizationUri, null, Credentials, null))
        {
            Service = (IOrganizationService)ServiceProxy;
        }
        return Service;
    }

错误在行中 EntityCollection collection = service.RetrieveMultiple(query); 任何人都可以帮我解决它

1 个答案:

答案 0 :(得分:0)

我遇到了类似的问题,并试图删除并再次添加microsoft.xrm.sdk.dll。 您在代码中提到的CRM的登录凭据可能不同或不正确。我之所以这样说是因为我遇到了同样的问题,并且发现我的登录凭据不正确,所以我更正了凭据并且工作正常。

谢谢, Shubham Grover