简体   繁体   中英

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

Hello experts, I am stuck at 'authentication to dynamics 365 with normal user'. I write a code in c# for authentication to dynamics 365. It works properly for the user who has admin of that organization but when I create a user into that org and sign up with the c# code it gives me 'Object reference not set to an instance of an object.' error on 'IOrganization service'.Below is my c# code for authenticating the user.

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;
        }
    }

what can I write extra code for authenticate with normal user using this code.

When you create a user, it has no security roles and can't do anything (including logging in). The error you get is what is seen in this scenario.

Make sure the new user has at least one security role.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM