integration openiddict identityServer .net core 7 with client .net framework 4.8

25 Views Asked by At

I have a identityServer with the following configuration written with .net core 7

    builder.Services.AddDbContext<IdentityServerDbContext>(options =>
{
    options.UseSqlServer(builder.Configuration.GetConnectionString("OAuthdb"));
    options.UseOpenIddict();
});
builder.Services.AddOpenIddict()
    .AddCore(options =>
    {
        options.UseEntityFrameworkCore()
               .UseDbContext<IdentityServerDbContext>();
    })

    .AddServer(options =>
    {

        options.SetAccessTokenLifetime(TimeSpan.FromDays(1));
        options.SetTokenEndpointUris("connect/token")
        .SetLogoutEndpointUris("connect/logout")
        .SetAuthorizationEndpointUris("connect/authorize");

        options.AllowAuthorizationCodeFlow();

        options.AddEncryptionKey(new SymmetricSecurityKey(Convert.FromBase64String("xxx")));

        options.AddDevelopmentEncryptionCertificate()
               .AddDevelopmentSigningCertificate();

        options.UseAspNetCore()
        .EnableAuthorizationEndpointPassthrough()
        .EnableLogoutEndpointPassthrough()
        .EnableTokenEndpointPassthrough();


        options.AllowAuthorizationCodeFlow()
                .AllowRefreshTokenFlow();
    })
    .AddValidation(options =>
    {
        options.UseLocalServer();
        options.UseAspNetCore();
    });

builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
    .AddCookie(c =>
    {
        c.LoginPath = "/Authenticate";
    });

and cofig seeder is:

new OpenIddictApplicationDescriptor
            {
                ClientId = "client48",
                ClientSecret = "901564A5-E7FE-42CB-B10D-61EF6A8F3652",
                ConsentType = ConsentTypes.Explicit,
                DisplayName = "client 48",
                RedirectUris =
                {
                    new Uri("https://localhost:44343/home/callback")
                },
                PostLogoutRedirectUris =
                {
                    new Uri("https://localhost:44343/home/logout")
                },
                Permissions =
                {
                    Permissions.Endpoints.Authorization,
                    Permissions.Endpoints.Logout,
                    Permissions.Endpoints.Token,
                    Permissions.GrantTypes.AuthorizationCode,
                    Permissions.ResponseTypes.Code,
                    Permissions.Scopes.Email,
                    Permissions.Scopes.Roles,
                    $"{Permissions.Prefixes.Scope}api1"
                },
                //Requirements =
                //{

                //    Requirements.Features.ProofKeyForCodeExchange
                //}
            }

And, I also have a client written with .net framework 4.8 and has the following code.

var oidcOptions = new OpenIdConnectAuthenticationOptions
{
    Authority = "https://localhost:7000",
    ClientId = "client48",
    ClientSecret = "901564A5-E7FE-42CB-B10D-61EF6A8F3652",
    PostLogoutRedirectUri = "https://localhost:44343/home/logout",
    RedirectUri = "https://localhost:44343/home/callback",
    ResponseType = OpenIdConnectResponseType.Code,
    Scope = "api1",
    TokenValidationParameters = new TokenValidationParameters
    {
        ValidAudiences = new string[]
                   {
                     "https://localhost:44343/",


                   },
        IssuerSigningKey = new SymmetricSecurityKey(Convert.FromBase64String("DRjd/GnduI3Efzen9V9BvbNUfc/VKgXltV7Kbk9sMkY="))
    },
    Notifications = new OpenIdConnectAuthenticationNotifications
    {

        AuthenticationFailed = async m =>
        {

        },
        AuthorizationCodeReceived = async n =>
        {
        },
        MessageReceived = async m =>
        {

        },
        SecurityTokenReceived = async m =>
        {

        },
        TokenResponseReceived = async m =>
        {

        },
        SecurityTokenValidated = m =>
        {

            return Task.FromResult(0);
        },
        RedirectToIdentityProvider = n =>
        {
            // if signing out, add the id_token_hint
            if (n.ProtocolMessage.RequestType == OpenIdConnectRequestType.Logout)
            {
                var idTokenHint = n.OwinContext.Authentication.User.FindFirst("id_token");

                if (idTokenHint != null)
                {
                    n.ProtocolMessage.IdTokenHint = idTokenHint.Value;
                }

            }

            return Task.FromResult(0);
        }

    }
};
app.UseOpenIdConnectAuthentication(oidcOptions);

app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

It always gives an error 401 and redirect to identity Server Do you think I did not make any settings? Do you think I did not make any settings? Do you think I did not make any settings? Do you think I did not make any settings? Do you think I did not make any settings?

0

There are 0 best solutions below