Identity/Authentication between ASP.Net MVC and Xamarin.Forms

Adrian N 81 Reputation points
2021-05-07T00:52:46.133+00:00

Hi All,

Just wondering if anyone has information on the best way to integrate Identity/Authentication ASP.NET MVC and Xamarin.
I've created a standard ASP.NET Core application using the latest framework with Individual Accounts, I would like to create a Xamarin.Forms application that uses the same database, and be able to manipulate this data as well for registering and sign in.

I can't seem to find any tutorials or documentation about this other than implementing an ASP.NET Web API application with Xamarin. So I'm left wondering if I should convert my MVC App into a .NET Framework with Web API, so I can just follow the tutorials directly.

Thanks in advance!

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,292 questions
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,148 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Ayomide Oluwaga 941 Reputation points
    2023-03-26T00:05:18.5633333+00:00

    Hello everyone,

    You can definitely use the same database and authentication system for both your ASP.NET MVC application and your Xamarin.Forms application. Here are some general steps you can follow to integrate authentication between the two:

    1. Expose an API endpoint from your ASP.NET MVC application that can authenticate users and return an access token. You can use ASP.NET Core Identity for this, which provides a simple way to manage user authentication and authorization.
    2. In your Xamarin.Forms application, create a login page where users can enter their credentials and authenticate with the API endpoint you created in step 1. You can use a library like Xamarin.Auth or IdentityModel.OidcClient to handle the authentication flow.
    3. Once the user is authenticated, store the access token securely in the Xamarin.Forms application. You can use the Xamarin.Essentials library to securely store the token in the device's keychain or secure storage.
    4. In any API requests you make from the Xamarin.Forms application, include the access token as an authorization header. Your ASP.NET MVC application can then use this token to authenticate and authorize the request.

    You don't necessarily need to convert your ASP.NET Core application into a .NET Framework with Web API to follow this approach. Instead, you can simply expose your ASP.NET Core application's authentication endpoints via an API controller, and use that endpoint in your Xamarin.Forms application.

    Overall, the approach you take will depend on the specifics of your application and what works best for your use case. But hopefully this gives you a general idea of how you can integrate authentication between your ASP.NET MVC and Xamarin.Forms applications.

    This should work, if it does, let me know:

    0 comments No comments