Connect ASP.NET MVC 4.6.2 project to IdentityServer4

I have a website running on ASP.NET MVC 4.5.2. I have an IdentityServer4 server running but when I try and authenticate against it I get an:

invalid_request

I googled a bit but I can’t find a solution. Finally, I found the way.

First, in your IdentityServer4 you have to create a new client:

public static IEnumerable GetClients() {
    return new List<client> {
        new Client {
            ClientId = "yourid",
            AllowedScopes = new List<string> { "openid" },
            AllowedGrantTypes = GrantTypes.Hybrid,
            RedirectUris = new List { "https://yoururl/signin-oidc" },
        }
    }
}

When you added the new client, you can update your other MVC project. Under App_Start open Startup.Auth.cs and add this code:

using Microsoft.Owin.Security.Cookies;
using Microsoft.Owin.Security.OpenIdConnect;
using Owin;

namespace PSC
{
    public partial class Startup
    {
        public void ConfigureAuth(IAppBuilder app)
        {
            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = "Cookies"
            });

            app.UseOpenIdConnectAuthentication(
              new OpenIdConnectAuthenticationOptions
              {
                Authority = "https://yourIdentityServerUrl",
                ClientId = "yourid",
                ResponseType = "id_token code",
                SignInAsAuthenticationType = "Cookies",
                RedirectUri = "https://yoururl/signin-oidc",
                Scope = "openid",
              });
        }
    }
}

You have to add a Nuget package called Microsoft.Owin.Security.OpenIdConnect.

Happy coding!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.