我在使用 C# 向 Dynamics 365 普通用户进行身份验证时遇到错误

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

专家您好, 我卡在 'authentication to dynamics 365 with normal user'。我用 c# 编写了一个代码,用于对 dynamics 365 进行身份验证。它适用于拥有该组织管理员的用户,但是当我在该组织中创建一个用户并使用 c# 代码注册时,它给我 'Object reference not set to an instance of an object.' 错误'IOrganization service'。下面是我用于验证用户的 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;
        }
    }

我可以编写什么额外的代码来使用此代码对普通用户进行身份验证。

当您创建用户时,它没有安全角色并且不能执行任何操作(包括登录)。您得到的错误是在这种情况下看到的。

确保新用户至少拥有一个安全角色。