xamarin / Essentials

Xamarin.Essentials is no longer supported. Migrate your apps to .NET MAUI, which includes Maui.Essentials.
https://aka.ms/xamarin-upgrade
Other
1.52k stars 506 forks source link

Xamarin forms iOS project with Apple Sign-in and ASP.NET API server as a backend #2015

Closed NikolaiTsepalov closed 2 years ago

NikolaiTsepalov commented 2 years ago

I'm working on a Xamarin forms IOs application. As the authentication component, I use the "Azure Active Directory B2C, and access a Web API with the resulting tokens". https://github.com/Azure-Samples/active-directory-b2c-xamarin-native It did not pass Apple store submission; they want me to use the native "Sign in with Apple".

I was able to configure the client quite easily: webAuthenticatorResult = await AppleSignInAuthenticator.AuthenticateAsync(); AuthToken = webAuthenticatorResult?.AccessToken ?? webAuthenticatorResult?.IdToken; I'm getting this authentication token.

The server project uses AspNet.Security.OAuth.Apple, but very old (3. something) version. In my test, I use the latest 6.0.6 I use the same service configuration as in the sample project: services.AddAuthentication(o => { o.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme; }) .AddCookie () .AddApple(a => { a.ClientId = "XXX.Service"; // Looks like at has to be service // a.ClientId = "XXX.App"; a.KeyId = "8NWK7GUXXX"; a.TeamId = "A82HCAHXXX"; a.UsePrivateKey(keyId => WebHostEnvironment.ContentRootFileProvider.GetFileInfo($"AuthKey_{keyId}.p8")); a.SaveTokens = true; }); Looks like the ClientId has to be the service ID, not the client ID. The main question: is Apple Auth require the use of the MobileAuthController? https://github.com/xamarin/Essentials/blob/develop/Samples/Sample.Server.WebAuthenticator/Controllers/MobileAuthController.cs Strange thing: The first call to my API https://xxxwebapitest.azurewebsites.net/api/user Trying to redirect to https://xxxwebapitest.azurewebsites.net/Account/Login?ReturnUrl=%2Fapi%2Fuser The default value used by CookieAuthenticationMiddleware PathString("/Account/Login"); I tried two things: 1 Point it to middleware call back: .AddCookie (o=> o.LoginPath = "signin-apple" ) 2 Changing the Route in the controller to Account/Login, basically trying to use the controller. The "best" thing I've got so far is the query in the following method: protected override string BuildChallengeUrl If manually used in the browser: https://appleid.apple.com/auth/authorize?client_id=XXX.SignInWithApple.Service&scope=openid%20email&response_type=code&redirect_uri=https%3A%2F%2FXXXwebapitest.azurewebsites.net%2Fsignin-apple&state=CXXXQ&response_mode=form_post It shows the web Apple login page, but after entering the password, it gives https://xxxwebapitest.azurewebsites.net/signin-apple HTTP ERROR 500

It would be nice if somebody made/pointed to a solid example of using the Xamarin forms iOS project with Apple Sign-in and ASP.NET API server as a backend. Thanks, Nikolai

NikolaiTsepalov commented 2 years ago

Dup https://github.com/aspnet-contrib/AspNet.Security.OAuth.Providers/issues/683