Red
Red

Reputation: 35

How to configure an mvc client running on .Net Framework 4.7.1 to Authenticate with IdentityServer4 (3.1) running on .Net Core

I am not sure how to configure an mvc client running on .Net Framework 4.7.1 to Authenticate with IdentityServer4 (3.1) running on .Net Core.

I have successfully authenticated clients running on .net core against IdentityServer4 before but not a client running on .Net Framework. I can't upgrade this client to .net core unfortunately.

Basically, I am not sure how to do this on the mvc client:

public void ConfigureServices(IServiceCollection services)
    {
        services.AddControllersWithViews();

        JwtSecurityTokenHandler.DefaultMapInboundClaims = false;

        services.AddAuthentication(options =>
        {
            options.DefaultScheme = "Cookies";
            options.DefaultChallengeScheme = "oidc";
        })
        .AddCookie("Cookies")
        .AddOpenIdConnect("oidc", options =>
        {
            options.Authority = "https://myIdentityServer:4532";

            options.ClientId = "MVC_Net_Framework";
            options.ClientSecret = "mysecret";
            options.ResponseType = "code";
            
            options.Scope.Add("myScope");

            options.SaveTokens = true;
        });
    }

Upvotes: 0

Views: 922

Answers (1)

NAS
NAS

Reputation: 331

you need to use OwinStartup class . add partial class Startup in root of your project as

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using Microsoft.Owin;
using Owin;
using Microsoft.Owin.Cors;
using System.Web.Http;
using System.Web.Mvc;
using System.Configuration;
[assembly: OwinStartup(typeof(MCVAppNet7.Startup))]
namespace MCVAppNet7
{
    public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            var services = new ServiceCollection();

            System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
            ConfigureAuth(app);

            // For Access-Control-Allow-Origin
            app.UseCors(CorsOptions.AllowAll);

        }
    }

}

after this create a new file "Startup.Auth.cs" in "App_Start" folder and inside this create partial Startup class

using System.Configuration;
using Owin;
using Microsoft.Owin.Security.Cookies;
using IdentityServer3.AccessTokenValidation;
using System;

namespace MCVAppNet7
{
    public partial class Startup
    {
        public void ConfigureAuth(IAppBuilder app)
        {
            try
            {
                app.UseCookieAuthentication(new CookieAuthenticationOptions
                {
                    AuthenticationType = "Cookies"
                });
                
                app.UseIdentityServerBearerTokenAuthentication(new IdentityServerBearerTokenAuthenticationOptions
                {
                    Authority = "",
                    ClientId = "",
                    AuthenticationType = "Bearer",
                    RequiredScopes = new[] { "" },
                    ValidationMode = "",
                    PreserveAccessToken = true,
                    RequireHttps = ""
                });
            }

            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}

install these package from from NuGet

  • Microsoft.Owin
  • Microsoft.Owin.Security.OAuth
  • Microsoft.Owin.Host.SystemWeb
  • IdentityModel
  • IdentityServer3.Contrib.AccessTokenValidation

I'm using IdentityServer3.Contrib.AccessTokenValidation and it's working for me but it might work with IdentityServer4.AccessTokenValidation and more info here

Upvotes: 1

Related Questions