Home

Awesome

Unity3D Android/iOS Authentication Example

Mobile applications that need OpenID Connect / OAuth 2 authentication would normally use a library like Google's AppAuth, which is available for Android and iOS, but unfortunatley not for Unity Android/iOS apps.

Luckily, there's a C# library that does work: IdentityModel.OidcClient2. This is not just a drop-in package, however - there are quite a few steps to configure your Unity project to use it successfully.
But it's not too onerous, and the end result is your app will use SFSafariViewController on iOS and Chrome Custom Tabs on Android, and be able to work with any OAuth 2 / OpenID Connect server.

This repository contains an example Unity 2018.1 Android/iOS app that demonstrates how this can be achieved. It uses a demo instance of identityserver (https://demo.identityserver.io) - you can see the source code here. Vidoes of the example running: Android and iOS.

You can login with alice/alice or bob/bob

Unity configuration notes:

Unity Scene configuration

UnitySendMessage("SignInCanvas", "OnAuthReply", queryString);

So it's expected that your sign-in scene has a GameObject called SignInCanvas that has a script attached with function OnAuthReply, as demonstrated in the example scene in this repo.

iOS support

Android support

Important: Unity Version Update

References

Two critical blog posts that enabled me to work out how to achieve this:

Code samples using IdentityModel.OidcClient2 for other platforms here.