Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

401 error after upgrading to version 9

Posted on by Microsoft Employee

Hi,

I am trying to get a .net website to connect to an instance of Dynamics that has recently been upgraded to version 9 (1710 (9.0.2.1470)). I have updated my connection string and installed the latest version of Microsoft.CrmSdk.CoreAssemblies. The solution builds but when i run it locally and it tries to communicate with Dynamics is get an error (see below) I think this is ultimately being caused by the crm returning a 401 error when the site tries to connect to it. i get the same error when i try to use CrmSvcUtil.

Can anyone help me to work out what might be wrong?

Server Error in '/' Application.


Authentication failed because the remote party has closed the transport stream.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.IO.IOException: Authentication failed because the remote party has closed the transport stream.

Source Error: 

Line 1496:            stopWatch.Start();
Line 1497:
Line 1498:            var contact = xrm.ContactSet.FirstOrDefault(x => x.Id == contactId);
Line 1499:
Line 1500:            stopWatch.Stop();


Source File: D:\Sites\HD-CrmUpgrade\hearingdogs\HearingDogs.Crm\Account\ExternalAccount.cs    Line: 1498 

Stack Trace: 

[IOException: Authentication failed because the remote party has closed the transport stream.]
   System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest) +8343534
   System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest) +186
   System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest) +345
   System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest) +203
   System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult) +769
   System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) +355
   System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) +20
   System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) +82
   System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result) +1105
   System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size) +60
   System.Net.ConnectStream.WriteHeaders(Boolean async) +463

[WebException: The underlying connection was closed: An unexpected error occurred on a send.]
   System.Net.HttpWebRequest.GetResponse() +1686
   System.ServiceModel.Description.MetadataLocationRetriever.DownloadMetadata(TimeoutHelper timeoutHelper) +127
   System.ServiceModel.Description.MetadataRetriever.Retrieve(TimeoutHelper timeoutHelper) +64

[InvalidOperationException: Metadata contains a reference that cannot be resolved: 'https://hdtest.crm4.dynamics.com/XRMServices/2011/Organization.svc?wsdl&sdkversion=9'.]
   System.ServiceModel.Description.MetadataRetriever.Retrieve(TimeoutHelper timeoutHelper) +357
   System.ServiceModel.Description.MetadataExchangeClient.ResolveNext(ResolveCallState resolveCallState) +179
   System.ServiceModel.Description.MetadataExchangeClient.GetMetadata(MetadataRetriever retriever) +147
   Microsoft.Xrm.Sdk.Client.ServiceMetadataUtility.RetrieveServiceEndpointMetadata(Type contractType, Uri serviceUri, Boolean checkForSecondary) +1350
   Microsoft.Xrm.Sdk.Client.ServiceConfiguration`1..ctor(Uri serviceUri, Boolean checkForSecondary) +150
   Microsoft.Xrm.Sdk.Client.OrganizationServiceConfiguration..ctor(Uri serviceUri, Boolean enableProxyTypes, Assembly assembly) +316
   Microsoft.Xrm.Sdk.Client.ServiceConfigurationFactory.CreateConfiguration(Uri serviceUri, Boolean enableProxyTypes, Assembly assembly) +266
   Microsoft.Xrm.Client.Services.OrganizationService.CreateServiceConfiguration(CrmConnection connection) +204
   Microsoft.Xrm.Client.Services.OrganizationService.GetServiceConfiguration(CrmConnection connection) +252
   Microsoft.Xrm.Client.Services.OrganizationService.ToOrganizationServiceProxy(CrmConnection connection) +85
   Microsoft.Xrm.Client.Services.OrganizationService.ToOrganizationService(CrmConnection connection) +14
   System.Lazy`1.CreateValue() +708
   System.Lazy`1.LazyInitValue() +184
   Microsoft.Xrm.Client.Services.InnerOrganizationService.UsingService(Func`2 action) +110
   Microsoft.Xrm.Sdk.Client.OrganizationServiceContext.Execute(OrganizationRequest request) +224
   Microsoft.Xrm.Sdk.Linq.QueryProvider.RetrieveEntityCollection(OrganizationRequest request, NavigationSource source) +367
   Microsoft.Xrm.Sdk.Linq.QueryProvider.Execute(QueryExpression qe, Boolean throwIfSequenceIsEmpty, Boolean throwIfSequenceNotSingle, Projection projection, NavigationSource source, List`1 linkLookups, String& pagingCookie, Boolean& moreRecords) +569
   Microsoft.Xrm.Sdk.Linq.QueryProvider.Execute(QueryExpression qe, Boolean throwIfSequenceIsEmpty, Boolean throwIfSequenceNotSingle, Projection projection, NavigationSource source, List`1 linkLookups) +121
   Microsoft.Xrm.Sdk.Linq.QueryProvider.Execute(Expression expression) +277
   Microsoft.Xrm.Sdk.Linq.QueryProvider.System.Linq.IQueryProvider.Execute(Expression expression) +72
   HearingDogs.Crm.Account.ExternalAccount.GetContact(XrmServiceContext xrm, Guid contactId) in D:\Sites\HD-CrmUpgrade\hearingdogs\HearingDogs.Crm\Account\ExternalAccount.cs:1498
   HearingDogs.Crm.Account.<>c__DisplayClass7_0.<GetProfile>b__0() in D:\Sites\HD-CrmUpgrade\hearingdogs\HearingDogs.Crm\Account\ExternalAccount.cs:377
   System.Threading.Tasks.Task`1.InnerInvoke() +76
   System.Threading.Tasks.Task.Execute() +70

[AggregateException: One or more errors occurred.]
   System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification) +4757600
   HearingDogs.Crm.Account.ExternalAccount.GetProfile(Guid userId) in D:\Sites\HD-CrmUpgrade\hearingdogs\HearingDogs.Crm\Account\ExternalAccount.cs:436
   HearingDogs.Web.Business.Profile.ProfileService.GetProfileByUserId(Guid userId) in D:\Sites\HD-CrmUpgrade\hearingdogs\HearingDogs.Web\Business\Profile\ProfileService.cs:68
   HearingDogs.Web.Business.Profile.ProfileService.GetProfile() in D:\Sites\HD-CrmUpgrade\hearingdogs\HearingDogs.Web\Business\Profile\ProfileService.cs:48
   HearingDogs.Web.Models.ViewModels.Blocks.UserContactPermissionsOptOutBlockViewModel..ctor(UserContactPermissionsOptOutBlock currentBlock, AccountMarketingDetailsSubmissionModel submissionModel, IProfileService profileService, IAccountService accountService, IBaseDependencies baseDependencies) in D:\Sites\HD-CrmUpgrade\hearingdogs\HearingDogs.Web\Models\ViewModels\Blocks\UserContactPermissionsOptOutBlockViewModel.cs:24
   HearingDogs.Web.Controllers.Blocks.UserContactPermissionsOptOutBlockController.Index(UserContactPermissionsOptOutBlock currentBlock) in D:\Sites\HD-CrmUpgrade\hearingdogs\HearingDogs.Web\Controllers\Blocks\UserContactPermissionsOptOutBlockController.cs:31
   lambda_method(Closure , ControllerBase , Object[] ) +142
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +229
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +35
   System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +39
   System.Web.Mvc.Async.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) +77
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +42
   System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +72
   System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +387
   System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +387
   System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +387
   System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +387
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +42
   System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +38
   System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +188
   System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +38
   System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +29
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +73
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +52
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +39
   System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +38
   System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +43
   System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +73
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +38
   System.Web.Mvc.<>c__DisplayClass4.<Wrap>b__3() +18
   System.Web.Mvc.ServerExecuteHttpHandlerWrapper.Wrap(Func`1 func) +29
   System.Web.HttpServerUtility.ExecuteInternal(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage, VirtualPath path, VirtualPath filePath, String physPath, Exception error, String queryStringOverride) +1706

[HttpException (0x80004005): Error executing child request for handler 'System.Web.Mvc.HttpHandlerUtil+ServerExecuteHttpHandlerAsyncWrapper'.]
   System.Web.HttpServerUtility.ExecuteInternal(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage, VirtualPath path, VirtualPath filePath, String physPath, Exception error, String queryStringOverride) +2320
   System.Web.HttpServerUtility.Execute(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage) +131
   System.Web.HttpServerUtility.Execute(IHttpHandler handler, TextWriter writer, Boolean preserveForm) +33
   System.Web.Mvc.Html.ChildActionExtensions.ActionHelper(HtmlHelper htmlHelper, String actionName, String controllerName, RouteValueDictionary routeValues, TextWriter textWriter) +573
   EPiServer.Web.Mvc.PartialRequest.RenderAction(HtmlHelper helper, String action, String controller, Object routeValues) +66
   EPiServer.Web.Mvc.Html.IContentDataExtensions.RenderContentData(HtmlHelper html, IContentData contentData, Boolean isContentInContentArea, TemplateModel templateModel, IContentRenderer contentRenderer) +569
   EPiServer.Web.Mvc.Html.ContentAreaRenderer.RenderContentAreaItem(HtmlHelper htmlHelper, ContentAreaItem contentAreaItem, String templateTag, String htmlTag, String cssClass) +1373
   EPiServer.Web.Mvc.Html.ContentAreaRenderer.RenderContentAreaItems(HtmlHelper htmlHelper, IEnumerable`1 contentAreaItems) +193
   EPiServer.Web.Mvc.Html.ContentAreaRenderer.Render(HtmlHelper htmlHelper, ContentArea contentArea) +344
   ASP.util_views_shared_displaytemplates_contentarea_ascx.__Render__control1(HtmlTextWriter __w, Control parameterContainer) in D:\Sites\HD-CrmUpgrade\hearingdogs\HearingDogs.Web\Util\Views\Shared\DisplayTemplates\ContentArea.ascx:4
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +117
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +79
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +250
   System.Web.Mvc.ViewPage.Render(HtmlTextWriter writer) +75
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +79
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +8753

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: 401 error after upgrading to version 9

    Thanks David,

    You were right, the issue was that my code was not using TLS 1.2 by default. I added the following to the constructor of the class that calls the Xrm and its is now able to connect correctly:

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

    thanks for your help!

    Sam

  • Verified answer
    David Jennaway Profile Picture
    David Jennaway 14,063 on at
    RE: 401 error after upgrading to version 9

    The most likely cause is your code may be trying to use a version of TLS prior to TLS 1.2. CRM 9.x only supports TLS 1.2 or higher, whereas previous CRM versions supported TLS 1.0 and 1.1. Things to check are:

    • Does your machine have TLS 1.2 configured (some older versions of Windows don't). Use a tool like https://www.ssllabs.com/ssltest/viewMyClient.html to check this
    • Check your .Net version. .Net 4.6.2 onwards should use TLS 1.2 by default, but earlier versions won't. Note there's a gotcha when changing the .Net version then debugging in Visual Studio; the .Net version may not update for debugging. To force this, delete the .vhost. files in the bin directory and subdirectories

    If you can't use .Net 4.6.2 or higher, then there are a couple of other options:

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!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans
Liquid error: parsing "/forums/thread/details/?threadid=%27nvOpzp;%20AND%201=1%20OR%20(%3C%27%22%3EiKO))," - Too many )'s.