Microsoft Single-Sign-On windows form


Recently I found myself looking for a way to apply single sign on (SSO) for a windows form, since we are implementing SSO across our application, and ran across this repository: https://github.com/rgregg/microsoft-account-winforms
It had the majority of what I needed except it was using the OneDrive API instead of the desired Microsoft Identity Platform. So I decided to do my first public fork. Here is a list of the changes I made:
Switched to Microsoft Identity Platform
It retrieves the email of the signed in account
Added an option to prompt for
select_account
The basic usage of the app can be found in the repository here: https://github.com/ShayneRC/microsoft-account-winforms
Include the .dll
in your windows form project and call it like this:
using MicrosoftAccount.WindowsForms;
public async void Authenticate()
{
bool selectAccount = true;
var result = await MicrosoftAccountOAuth.LoginAuthorizationCodeFlowAsync("client_id", new string[] { "https://graph.microsoft.com/user.read", "offline_access" }, selectAccount);
string accessToken = result.AccessToken;
string refreshToken = result.RefreshToken;
string userEmail = result.Email;
}
In the LoginAuthorizationCodeFlowAsync()
method a form will popup asking the user to log into their Microsoft account using the client_id
to determine the application to log in to. With selectAccount
as false
, if a user is already logged in then it will skip past the account selection and automatically sign them in.
If you want to improve on my solution feel free to reach out or start a pull request on GitHub, I hope this helps someone!
Subscribe to my newsletter
Read articles from Shayne Kiekebosch directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Shayne Kiekebosch
Shayne Kiekebosch
.NET developer specialising in Windows Mixed Reality and Windows forms/services. Utilising Unity I have developed applications for the Microsoft HoloLens 1 and 2 and the Meta Quest 1, 2 and Pro. I was also a senior dev at Blackout Games working on their Rugby Management Game.