Quantcast
Channel: Microsoft Dynamics CRM Forum - Recent Threads
Viewing all articles
Browse latest Browse all 55831

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

$
0
0

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.

Viewing all articles
Browse latest Browse all 55831


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>