IdentityServer4.Validation.AuthorizeRequestValidator[0] "redirect_uri is missing or too long"

Viewed 2109

I am setting up a security server using IdentityServer4 and asp.netcore 2.2. I want to authenticate using Azure AD and keep my user data using Asp.Net IdentityCore. I have setup external authentication using AddMicrosoftAccount. Authentication works well but i am always getting "IdentityServer4.Validation.AuthorizeRequestValidator[0] redirect_uri is missing or too long". I have debugged the code and noticed that the return url is passed correctly in the querystring to Login.cshtml.cs. the returnUrl parameter to the OnGetAsync method however does not have the return url. it looks like its being striped off somewhere. Please help

I am using IdentityServer4, SQL Server for configuration and Operational data, Sql server for user data using Asp.NetCoreIdentity. I have created an mvc client and registered it to the server. I have added an authorize attribute to the Home controller of the client application to trigger the authentication.

//my server client registration code

var webClient = new IdentityServer4.EntityFramework.Entities.Client
{
    ClientId = "mvc",
    ClientName = "MVC Client",
    Description = "Test MVC Client",
    RequireConsent = false,
    AlwaysIncludeUserClaimsInIdToken = true,
    AllowedScopes = new List<ClientScope>
    {
        new ClientScope
        {                       
            Scope = IdentityServerConstants.StandardScopes.OpenId 
        },
        new ClientScope
        {
            Scope = IdentityServerConstants.StandardScopes.Profile
        }
    }
};

//set the grant type
webClient.AllowedGrantTypes = new List<ClientGrantType>
{
    new ClientGrantType
    {
        GrantType = GrantType.Implicit,
        Client = webClient
    }
};

// where to redirect to after login
webClient.RedirectUris = new List<ClientRedirectUri>
{
    new ClientRedirectUri
    {
        RedirectUri = "http://localhost:5000/signin-oidc",
        Client = webClient
    }
};

// where to redirect to after logout
webClient.PostLogoutRedirectUris = new List<ClientPostLogoutRedirectUri>               
{
    new ClientPostLogoutRedirectUri
    {
      PostLogoutRedirectUri="http://localhost:5000/signout-callback-oidc",                       
      Client = webClient
    }
};


//Client configuration
services.AddAuthentication(options => 
{
    options.DefaultScheme = "Cookies";
    options.DefaultChallengeScheme = "oidc";
})
.AddCookie("Cookies")
.AddOpenIdConnect("oidc", options =>
{
    options.Authority = "http://localhost:5010";
    options.RequireHttpsMetadata = false;
    options.ClientId = "mvc";
    options.ClientSecret = "client secret".ToSha256();
    options.SaveTokens = true;
    options.CallbackPath = "/signin-oidc";                
});  

i have added app.UseAuthentication() to configure method

I expect the security server to redirect back to my client Home controller method after successful authentication. Instead i am getting the above error. what am i missing

1 Answers

You have not configured any RedirectUris in your client's configuration. You need to add at least one redirect uri.

RedirectUris = {"http://<your redirect uri>"}

For example:

var webClient = new IdentityServer4.EntityFramework.Entities.Client
{
    ClientId = "mvc",
    ClientName = "MVC Client",
    Description = "Test MVC Client",
    RequireConsent = false,
    AlwaysIncludeUserClaimsInIdToken = true,
    RedirectUris = {"http://<your redirect uri>"}
    AllowedScopes = new List<ClientScope>
    {
        new ClientScope
        {                       
            Scope = IdentityServerConstants.StandardScopes.OpenId 
        },
        new ClientScope
        {
            Scope = IdentityServerConstants.StandardScopes.Profile
        }
    }
};
Related