Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

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

Posted on by 75

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.

*This post is locked for comments

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: I am getting error on authenticate to dynamics 365 normal user with c#.

    Ok. Once following line is called

    CrmServiceClient GetCrmServiceClient = new CrmServiceClient(@"AuthType=Office365;Username='" + username + "'; Password='" + password + "';Url='" + url + "'");

    Check CrmServiceClient's properties IsReady (that will have true if connection was successful and false if it was not), LastCrmError and LastCrmException will contain explanation what went wrong.

  • Kiran_girase Profile Picture
    Kiran_girase 75 on at
    RE: I am getting error on authenticate to dynamics 365 normal user with c#.

    Hi andrew butenko,

    service = (IOrganizationService)GetCrmServiceClient.OrganizationWebProxyClient != null ? (IOrganizationService)GetCrmServiceClient.OrganizationWebProxyClient : (IOrganizationService)GetCrmServiceClient.OrganizationServiceProxy;

    Service object getting null.

  • a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: I am getting error on authenticate to dynamics 365 normal user with c#.

    Hello,

    What exact line generates an error?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans