More .NET libraries
-
Rebex Mail Pack
IMAP, MS Graph, EWS, POP3, SMTP, MIME, S/MIME, MSG
-
Rebex Total Pack
All Rebex .NET libraries together
Back to feature list...
Authentication modes
On this page:
Username and password
Password-based authentication is simple:
// create client instance and connect // ... // log in client.Login(username, password);
' create client instance and connect ' ... ' log in client.Login(username, password)
OAuth 2.0 authentication
Cloud-based Exchange servers (Microsoft 365 / Outlook Online) support OAuth 2.0 authentication mechanism.
To log in using OAuth, an application (or its user) authenticates via the service provider's authentication server and receives an access token. Then it connects to the Exchange server and authenticates using the obtained token:
// create client instance and connect // ... // authenticate using OAuth 2.0 access token client.Login(userEmail, accessToken, EwsAuthentication.OAuth20);
' connect to a server Dim client = New Rebex.Net.Ews() client.Connect("smtp.gmail.com", SslMode.Implicit) ' authenticate using OAuth 2.0 access token client.Login(userEmail, accessToken, EwsAuthentication.OAuth20)
The process of obtaining the access token is slightly different for each provider and application type. For details, see the following articles:
- Office 365: How to register your app (for apps with signed-in users)
- Office 365: How to use OAuth 2.0 with Rebex Mail (for apps with signed-in users)
- Office 365 and EWS in unattended (app-only) mode (for services and daemons)
Client certificate authentication
Client certificates are an optional way to authenticate the client to the server. This is only possible when connecting/authenticating to a TLS/SSL-capable server. However, most servers still require authentication with a username and password even when client certificate authentication has taken place.
A certificate with an associated private key is needed for client authentication. Set Settings.SslClientCertificateRequestHandler
property
to an implementation of certificate request handler that is called when the server asks for client certificate.
a) Use the built-in StoreSearch
handler, that searches the user's certificate store for a first suitable certificate:
// create client instance // ... // set a certificate request handler client.Settings.SslClientCertificateRequestHandler = CertificateRequestHandler.StoreSearch; // connect to the server client.Connect(hostname, SslMode.Explicit); // authenticate (still needed in many cases) if (!client.IsAuthenticated) client.Login(username, password);
' create client instance ' ... ' set a certificate request handler client.Settings.SslClientCertificateRequestHandler = CertificateRequestHandler.StoreSearch ' connect to the server client.Connect(hostname, SslMode.Explicit) ' authenticate (still needed in many cases) If Not client.IsAuthenticated Then client.Login(username, password) End If
b) Use the built-in PFX-based certificate request handler:
// create client instance // ... // load a certificate chain from a .pfx/.p12 file CertificateChain certificate = CertificateChain.LoadPfx(@"C:\MyData\MyCertificate.pfx", "password"); // set a certificate request handler client.Settings.SslClientCertificateRequestHandler = CertificateRequestHandler.CreateRequestHandler(certificate); // connect to the server client.Connect(hostname, SslMode.Explicit); // authenticate (still needed in many cases) if (!client.IsAuthenticated) client.Login(username, password);
' create client instance ' ... ' load a certificate chain from a .pfx/.p12 file Dim certificate As CertificateChain = CertificateChain.LoadPfx("C:\MyData\MyCertificate.pfx", "password") ' set a certificate request handler client.Settings.SslClientCertificateRequestHandler = CertificateRequestHandler.CreateRequestHandler(certificate) ' connect to the server client.Connect(hostname, SslMode.Explicit) ' authenticate (still needed in many cases) If Not client.IsAuthenticated Then client.Login(username, password) End If
c) Write a custom handler, for example to load the certificate from a .pfx/.p12 file:
private class MyCertRequestHandler : ICertificateRequestHandler { // This method is called during TLS/SSL negotiation // when the server requests client certificate authentication public CertificateChain Request(TlsSocket socket, DistinguishedName[] issuers) { // provide a certificate loaded from a .pfx/.p12 file return CertificateChain.LoadPfx(clientCertPath, clientCertPassword); } }
Private Class MyCertRequestHandler Implements ICertificateRequestHandler ' This method is called during TLS/SSL negotiation ' when the server requests client certificate authentication Public Function Request(socket As TlsSocket, issuers As DistinguishedName()) As CertificateChain Implements ICertificateRequestHandler.Request ' provide a certificate loaded from a .pfx/.p12 file Return CertificateChain.LoadPfx(clientCertPath, clientCertPassword) End Function End Class
Don't forget to register the handler:
// create client instance // ... // set a certificate request handler client.Settings.SslClientCertificateRequestHandler = new MyCertRequestHandler(); // connect to the server client.Connect(hostname, SslMode.Explicit); // authenticate (still needed in many cases) if (!client.IsAuthenticated) client.Login(username, password);
' create client instance ' ... ' set a certificate request handler client.Settings.SslClientCertificateRequestHandler = New MyCertRequestHandler() ' connect to the server client.Connect(hostname, SslMode.Explicit) ' authenticate (still needed in many cases) If Not client.IsAuthenticated Then client.Login(username, password) End If
GSSAPI
GSSAPI support in Rebex EWS makes it possible to authenticate using Kerberos, NTLM or Negotiate authentication mechanisms, either in single sign-on mode or username/password(/domain)-based mode.
Note: GSSAPI is only supported on Windows platforms.
Single sign-on
With single sign-on, the current user can authenticate without having to enter his password. Single sign-on is only possible through NTLM or through GSSAPI with Kerberos, NTLM or Negotiate authentication mechanisms on servers that support them. Additionally, both the client and server machines might have to be part of the same domain (or a domain trust has to be established).
Note: Single sign-on is only supported on Windows platforms.
Kerberos authentication
EWS and Kerberos in single sign-on mode:
// create client instance and connect (Rebex.Net.Ews) // ... // log in using Kerberos single sign-on client.Login(EwsAuthentication.Kerberos);
' create client instance and connect (Rebex.Net.Ews) ' ... ' log in using Kerberos single sign-on client.Login(EwsAuthentication.Kerberos)
EWS and Kerberos with username/password/domain:
// create client instance and connect (Rebex.Net.Ews) // ... // log in using Kerberos client.Login(username + "@" + domain, password, EwsAuthentication.Kerberos);
' create client instance and connect (Rebex.Net.Ews) ' ... ' log in using Kerberos client.Login(username & "@" & domain, password, EwsAuthentication.Kerberos)
Note: Kerberos is only supported on Windows platforms. However, it's possible to authenticate Windows-based clients to Unix-based servers using Kerberos.
NTLM authentication
EWS and NTLM single sign-on mode:
// create client instance and connect (Rebex.Net.Ews) // ... // log in using Ntlm single sign-on client.Login(EwsAuthentication.Ntlm);
' create client instance and connect (Rebex.Net.Ews) ' ... ' log in using Ntlm single sign-on client.Login(EwsAuthentication.Ntlm)
EWS and NTLM with username/password/domain:
// create client instance and connect (Rebex.Net.Ews) // ... // log in using Ntlm client.Login(username + "@" + domain, password, EwsAuthentication.Ntlm);
' create client instance and connect (Rebex.Net.Ews) ' ... ' log in using Ntlm client.Login(username & "@" & domain, password, EwsAuthentication.Ntlm)
Note: On non-Windows platforms (Linux, Android, macOS, iOS), NTLM is only available with NTLM plugin.
Negotiate authentication
EWS and Negotiate in single sign-on mode:
// create client instance and connect (Rebex.Net.Ews) // ... // log in using Negotiate single sign-on client.Login(EwsAuthentication.Negotiate);
' create client instance and connect (Rebex.Net.Ews) ' ... ' log in using Negotiate single sign-on client.Login(EwsAuthentication.Negotiate)
EWS and Negotiate with username/password/domain:
// create client instance and connect (Rebex.Net.Ews) // ... // log in using Negotiate client.Login(username + "@" + domain, password, EwsAuthentication.Negotiate);
' create client instance and connect (Rebex.Net.Ews) ' ... ' log in using Negotiate client.Login(username & "@" & domain, password, EwsAuthentication.Negotiate)
Note: Negotiate is only supported on Windows platforms.
Back to feature list...