Azure / azure-notificationhubs-dotnet

.NET SDK for Azure Notification Hubs
MIT License
70 stars 119 forks source link

[BUG] Sending Notification: Could not create SSL/TLS secure channel #183

Closed TheOfficialLoren closed 3 years ago

TheOfficialLoren commented 3 years ago

Describe the bug When sending a notification I receive an exception: Could not create SSL/TLS secure channel

Exception or Stack Trace

Microsoft.Azure.NotificationHubs.Messaging.MessagingException: Unexpected exception encountered TrackingId:c03e475f-c91d-4ec7-bcc1-3374c938738a,TimeStamp:2021-01-14T22:19:33.5028410Z ---> System.Net.Http.HttpRequestException: An error occurred while sending the request. ---> System.Net.WebException: The request was aborted: Could not create SSL/TLS secure channel. at System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult, TransportContext& context) at System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult ar) --- End of inner exception stack trace --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Azure.NotificationHubs.NotificationHubClient.d201.MoveNext() --- End of inner exception stack trace --- at Microsoft.Azure.NotificationHubs.ExceptionsUtility.HandleUnexpectedException(Exception ex, String trackingId) at Microsoft.Azure.NotificationHubs.NotificationHubClient.d201.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Azure.NotificationHubs.NotificationHubClient.<>c__DisplayClass189_0.<b0>d.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Azure.NotificationHubs.NotificationHubRetryPolicy.d41.MoveNext() --- End of stack trace from previous location where exception was thrown --- at Microsoft.Azure.NotificationHubs.NotificationHubRetryPolicy.<RunOperation>d__41.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Azure.NotificationHubs.NotificationHubClient.d189.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at I1.Service.NotificationService.d5.MoveNext() in C:\Users\loren\dev\ai\Application\Service\I1.Service\NotificationService.cs:line 44

To Reproduce

Just send a notification using the code below. Use connection string from Azure Portal -> NotificationHubNamespace -> Access Policies Use notificationHubPath found in Azure Portal -> Notification Hub

Code Snippet

            string url = ConfigurationManager.AppSettings.Get("NotificationUrl");
            string hubName = ConfigurationManager.AppSettings.Get("NotificationHubName");
            Hub = NotificationHubClient.CreateClientFromConnectionString(url, hubName);

var iosMessage = $"{{\"aps\": {{\"alert\":{{\"title\":\"{notification.Title}\",\"body\":\"{notification.Body}\"}},\"sound\":\"default\" }} }}";
NotificationOutcome iosOutcome = await Hub.SendAppleNativeNotificationAsync(iosMessage, notification.UserNames.AsEnumerable<String>());

Expected behavior Message gets sent

Screenshots If applicable, add screenshots to help explain your problem.

Setup (please complete the following information): .Net Framework

Additional context

Information Checklist Kindly make sure that you have added all the following information above and checkoff the required fields otherwise we will treat the issuer as an incomplete report

TheOfficialLoren commented 3 years ago

After posting this, I found this article announcing a TLS update on Dec 31 2020:

https://docs.microsoft.com/en-us/azure/notification-hubs/notification-hubs-tls12

And another bug report in this github had a user suggesting adding this line: System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;

I added this line right before I instantiate the Notification Hub.

This resolved my issues.

amp64 commented 3 years ago

Thank you @TheOfficialLoren - same problem, same fix.

mpodwysocki commented 3 years ago

@TheOfficialLoren TLS 1.2 is the only supported protocol for Notification Hubs.