Microsoft Single-Sign-On windows form

Photo by Windows on Unsplash

Microsoft Single-Sign-On windows form

My first public repository ๐Ÿ‘€

ยท

2 min read

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!

ย