IdentityServer4 how to redirect the flow after login

5.8k Views Asked by At

I have installed an IdentityServer4 and a Client (Hybrid Mvc Client). All is ok. The following flow works:
1. User call secure page PageX (the controller is protected with Authorize attribute)
2. than system redirects the flow to Login page on IdentityServer
3. After authentication/authorization the IdentityServer redirect the user to url defined (redirect_uri) in the client configuration (page named Home) .

Now i don't know how to implement at the step 3 the redirection to PageX, the original page requested.

I have to create a custom AuthorizeAttribute to save on session storage the url of PageX and than using it in callback page? or is there any configuration on IdentityServer or client that could help me?

Thanks in advance

2

There are 2 best solutions below

0
On BEST ANSWER

This is typically what you’d use the state parameter for. Your callback will receive the state value back unaltered and then you can verify the URL within is local and redirect to it automatically.

I’d recommend protecting the value from tampering using the DataProtection features in .net.

0
On

After successful login, by default the IdentityServer middleware tries to redirect to a consent page where to inform the user for the "allowed scopes". In this page are shown the claims that the client mvc site will receive access to: user identifier, user profile, email etc. If you didn't setup such, you may set: "RequireConsent = false" when you define your MVC client. In such scenario the IdentityServer will redirect back to "RedirectUris" without showing consent page.

Example:

public static IEnumerable<Client> GetClients()
{
    return new List<Client>
    {
        new Client
        {
            ClientId = "mvc",
            ClientName = "mvc Client",
            ClientSecrets =
            {
                new Secret("secret".Sha256())
            },
            AllowedGrantTypes = GrantTypes.Implicit,
            AllowAccessTokensViaBrowser = true,
            RedirectUris = { "http://localhost:5002/signin-oidc" },
            PostLogoutRedirectUris = { "http://localhost:5002/signout-callback-oidc" },
            AllowedScopes =
            {
                IdentityServerConstants.StandardScopes.OpenId,
                IdentityServerConstants.StandardScopes.Profile,
                IdentityServerConstants.StandardScopes.Email
            },
            RequireConsent = false
        }
    };
}

The other thing that I've noticed in the IdentityServer4 demos and quick starts is that you need the following NuGet packages: For client website: IdentityModel, Microsoft.AspNetCore.All

For IdentityServer Authentication app: IdentityServer4, IdentityServer4.AccessTokenValidation, IdentityServer4.AspNetIdentity, Microsoft.AspNetCore.All

You may install these packages just to get the demo working.