Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CONNECT failed: RefusedNotAuthorized C# #63

Closed
CRACKbomber opened this issue Mar 1, 2017 · 20 comments
Closed

CONNECT failed: RefusedNotAuthorized C# #63

CRACKbomber opened this issue Mar 1, 2017 · 20 comments
Assignees
Labels
help wanted Extra attention is needed.

Comments

@CRACKbomber
Copy link

{Microsoft.Azure.Devices.Client.Exceptions.UnauthorizedException: CONNECT failed: RefusedNotAuthorized
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Azure.Devices.Client.Transport.Mqtt.MqttTransportHandler.

When I try to set method handlers I get the above inner exception.
csharpcode.txt

@tameraw tameraw added the help wanted Extra attention is needed. label Mar 1, 2017
@jasmineymlo
Copy link
Contributor

looks like the cert that you are using is not valid? It's authorization exception.

@CRACKbomber
Copy link
Author

CRACKbomber commented Mar 1, 2017

It is a valid cert. It is our root dc cert. From my understanding the hub only holds onto the x509 thumbprint and when you want to connect it compares the stored thumprint to the one supplied with the auth mechanism.

@rajeevmv
Copy link
Contributor

rajeevmv commented Mar 2, 2017

Can you try adding a receiveAsync call in your code after the following line ?
_client = DeviceClient.Create("pc-iothub01.azure-devices.net", certAuth, TransportType.Mqtt);
_client.ReceiveAsync(TimeSpan.FromSeconds(2)).Wait();

@CRACKbomber
Copy link
Author

Same thing, {"CONNECT failed: RefusedNotAuthorized"}

@rajeevmv
Copy link
Contributor

rajeevmv commented Mar 2, 2017

I have created a simple console app which uses X.509 certs and device methods. It works on my IoT Hub. Can you plug in the constant values at the top and try it out on your IoT hub and X.509 device?

X509-Mqtt-Methods.zip

@tameraw tameraw assigned rajeevmv and unassigned jasmineymlo Mar 3, 2017
@CRACKbomber
Copy link
Author

CRACKbomber commented Mar 7, 2017

Nope, same issue. I added the following to add the device and then try to auth. Still getting the same issue.

`var x509Certificate = GetX509Cert(CertSerial);

Device newDevice = new Device("TestingSSL");

newDevice.Authentication = new AuthenticationMechanism() { X509Thumbprint = new X509Thumbprint() { PrimaryThumbprint = x509Certificate.Thumbprint } };

RegistryManager registryManager = RegistryManager.CreateFromConnectionString(IotHubConnectionString);

await registryManager.AddDeviceAsync(newDevice);
Console.WriteLine("Retrieving Device using X.509 certificate for authentication");
var device = await registryManager.GetDeviceAsync("TestingSSL");`

@rajeevmv
Copy link
Contributor

rajeevmv commented Mar 8, 2017

From the service-side logs, it appears that the service is not receiving the client certificate.

Can you try changing the transport protocol to MQTT over Websocket like this:
_client = DeviceClient.Create("pc-iothub01.azure-devices.net", certAuth, TransportType.Mqtt_WebSocket_Only);

You could also try using another protocol like Amqp or Http:
__client = DeviceClient.Create("pc-iothub01.azure-devices.net", certAuth, TransportType.Amqp);
__client = DeviceClient.Create("pc-iothub01.azure-devices.net", certAuth, TransportType.Http1);

This will help us narrow down the issue that you are facing. Also, let us know which version of Windows the client is running on.

@CRACKbomber
Copy link
Author

image
Here is my windows info.

@CRACKbomber
Copy link
Author

_client = DeviceClient.Create("pc-iothub01.azure-devices.net", certAuth, TransportType.Mqtt_WebSocket_Only);
Same unauthorized message

__client = DeviceClient.Create("pc-iothub01.azure-devices.net", certAuth, TransportType.Amqp);
Message = "The method or operation is not implemented."

__client = DeviceClient.Create("pc-iothub01.azure-devices.net", certAuth, TransportType.Http1);
It seems to work but when I try to invoke a method from a service client it gives
Message = "Device {"Message":"ErrorCode:DeviceNotOnline;Timed out waiting for device to connect.","ExceptionMessage":"Tracking ID:14d6bb5f477048198af4840d9279d68f-G:2-TimeStamp:03/08/2017 12:17:57"} not registered"

@tameraw
Copy link
Contributor

tameraw commented Mar 8, 2017

@CRACKbomber - Can you please provide which version of the SDK your are using? Thanks.

@CRACKbomber
Copy link
Author

Microsoft.Azure.Devices.Client 1.2.4
Microsoft.Azure.Devices.Shared 1.0.7
Microsoft.Azure.Amqp 2.0.4

@rajeevmv
Copy link
Contributor

rajeevmv commented Mar 8, 2017

@CRACKbomber
Copy link
Author

@rajeevmv I have

@rajeevmv
Copy link
Contributor

rajeevmv commented Mar 8, 2017

@CRACKbomber ,

Can you share a self-signed cert that fails for you here? Please note that this should be a throwaway cert that you do not plan to use for any other purpose. I can try to use that cert using the sample I posted above and check if it works for me. I will need the private key as well in this case.

@CRACKbomber
Copy link
Author

There is no private key in my cert.
pc-iothub01.zip

@rajeevmv
Copy link
Contributor

rajeevmv commented Mar 9, 2017

I will need a pfx file (which contains the private key) along with a password to read the file. (FYI: https://security.stackexchange.com/questions/29425/difference-between-pfx-and-cert-certificates). This is because the client will need the private key in order to successfully complete the TLS handshake with the service.

@rajeevmv
Copy link
Contributor

rajeevmv commented Mar 9, 2017

This appears to be the root cause of the issue that you are facing. You will need to provide a .pfx file as input to the device client. The .pfx file should contain the private key of the cert generated. This private key will not be transmitted over the wire. But, it will be used to prove to the server that it is indeed the owner of the cert.

Our apologies that the documentation does not state this explicitly. We will update it to reflect this.

@tameraw
Copy link
Contributor

tameraw commented Mar 13, 2017

@CRACKbomber - Closing issue. Please let us know if still having problems.

@tameraw tameraw closed this as completed Mar 13, 2017
@awong1900
Copy link

@rajeevmv Can you tell me how o make .pfx file. Any step by step helper link? I have same issue now.

@rajeevmv
Copy link
Contributor

anushprabhu pushed a commit to anushprabhu/azure-iot-sdk-csharp that referenced this issue Sep 13, 2018
Add client side tracing to the TPM over AMQP/WS scenario
CIPop pushed a commit to CIPop/azure-iot-sdk-csharp that referenced this issue Oct 5, 2018
Add client side tracing to the TPM over AMQP/WS scenario
CIPop pushed a commit to CIPop/azure-iot-sdk-csharp that referenced this issue Oct 8, 2018
Add client side tracing to the TPM over AMQP/WS scenario
CIPop pushed a commit to CIPop/azure-iot-sdk-csharp that referenced this issue Oct 9, 2018
Add client side tracing to the TPM over AMQP/WS scenario
CIPop pushed a commit to CIPop/azure-iot-sdk-csharp that referenced this issue Oct 9, 2018
Add client side tracing to the TPM over AMQP/WS scenario
CIPop added a commit that referenced this issue Oct 9, 2018
* Merge pull request #66 from Azure/alextolp/setcompletedsync

Setting CompletedSynchronously everywhere

* Provisioning MQTT transport using the ExecutorTaskScheduler.

* Device MQTT transport using the ExecutorTaskScheduler.

* Removing ConcurrentObjectPool.

* Merge pull request #63 from Azure/ravokkar/tpmamqpws-tracing

Add client side tracing to the TPM over AMQP/WS scenario

* Merge pull request #64 from Azure/alextolp/amqpsyncfix

AMQP - handle sync completion

* Merge pull request #65 from Azure/alextolp/amqpwsaddlogs

Add logs for amqp ws tpm

* Fixing synchronous completions for AMQP.

* Adding test execution note.

* Changing build order of netfx.
brycewang-microsoft pushed a commit that referenced this issue Jul 14, 2022
brycewang-microsoft pushed a commit that referenced this issue Jul 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed.
Projects
None yet
Development

No branches or pull requests

5 participants