Authenticate user on website using an API access token

Stesvis 1,041 Reputation points
2021-09-17T16:01:59.41+00:00

Hello,

I have a common scenario. A user is logged in to the MVC Web API using a mobile app (so the user has a valid access token) and therefore can consume any protected API endpoint with that token.

Some functionality is only available on the website, so the mobile app just redirects the user to the proper page on the website.

The website however redirects back to the login page because it uses Cookie authentication.

app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                ExpireTimeSpan = TimeSpan.FromDays(30),
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                LoginPath = new PathString("/Account/Login"),
                Provider = new CookieAuthenticationProvider
                {
                    // Enables the application to validate the security stamp when the user logs in.
                    // This is a security feature which is used when you change a password or add an
                    // external login to your account.
                    OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                        validateInterval: TimeSpan.Zero,
                        regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager)),
                },
            });

Is it possible to skip the login page since I have a valid token?
How can I implement it so that the token is passed in the URL or as a payload and the website can take that token and automatically log in the user and load the page directly?

Developer technologies ASP.NET Other
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2021-09-17T22:54:30.05+00:00

    you don't explain how the mobile calls the website.

    the website can support both bearer token and cookie tokens, by defining multiple authentication schemes.

    https://learn.microsoft.com/en-us/aspnet/core/security/authorization/limitingidentitybyscheme?view=aspnetcore-5.0


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.