簡體   English   中英

我在使用C#向Dynamics 365普通用戶進行身份驗證時遇到錯誤

[英]I am getting error on authenticate to dynamics 365 normal user with c#

您好專家,我被困在“普通用戶對Dynamics 365的身份驗證”上。 我用C#寫了一個代碼,用於對Dynamics 365進行身份驗證。該代碼對於擁有該組織管理員的用戶可以正常使用,但是當我在該組織中創建用戶並使用c#代碼進行注冊時,它為我提供了“對象引用未設置為對象的實例。” “ IOrganization服務”上的錯誤。下面是我的用於驗證用戶身份的C#代碼。

public AuthenticateUser authenticateUserByFetchXML(string URL, string username, string password)
    {
        try
        {
            // model class
            AuthenticateUser user = new AuthenticateUser();
            EntityReference resultRef = new EntityReference();

            IOrganizationService service;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

            //put input into service
            CrmServiceClient GetCrmServiceClient = new CrmServiceClient(@"AuthType=Office365;Username='" + username + "'; Password='" + password + "';Url='" + URL + "'");

            //get organization web client by Iorganization service
            service = (IOrganizationService)GetCrmServiceClient.OrganizationWebProxyClient != null ? (IOrganizationService)GetCrmServiceClient.OrganizationWebProxyClient : (IOrganizationService)GetCrmServiceClient.OrganizationServiceProxy;

            //Get fetchXML for system user
              string fetchuser = "<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>" +
                          "<entity name='systemuser'>" +
                            "<attribute name='fullname' />" +
                            "<attribute name='businessunitid' />" +
                            "<attribute name='title' />" +
                            "<attribute name='address1_telephone1' />" +
                            "<attribute name='positionid' />" +
                            "<attribute name='systemuserid' />" +
                            "<order attribute='fullname' descending='false' />" +
                          "</entity>" +
                        "</fetch>";
            EntityCollection users = new EntityCollection();
            users = service.RetrieveMultiple(new FetchExpression(fetchuser));

            if (users.Entities.Count > 0)
            {

                Guid gId = users.Entities[0].Id;
                 user.Id = gId;              
                return user;
            }
            return user;

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

我可以使用此代碼編寫額外的代碼以進行普通用戶身份驗證。

創建用戶時,它沒有安全角色,並且無法執行任何操作(包括登錄)。 您收到的錯誤是在這種情況下看到的。

確保新用戶至少具有一個安全角色。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM