如何将 .Net Core 的 Startup.cs 类翻译成新的 .NET Program.cs 类?

huangapple go评论77阅读模式
英文:

How do I translate a .Net Core Startup.cs class to the new .NET Program.cs class?

问题

I'm trying to translate a .NetCore project to a .Net 7 project.

The project uses the ITFoxTec SAML2 library.

The .NetCore project has a Startup.cs file that looks like this:

// .NetCore Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.BindConfig<Saml2Configuration>(Configuration, "Saml2", (serviceProvider, saml2Configuration) =>
    {
        saml2Configuration.AllowedAudienceUris.Add(saml2Configuration.Issuer);

        var httpClientFactory = serviceProvider.GetService<IHttpClientFactory>();
        var entityDescriptor = new EntityDescriptor();
        entityDescriptor.ReadIdPSsoDescriptorFromUrlAsync(httpClientFactory, new Uri(Configuration["Saml2:IdPMetadata"])).GetAwaiter().GetResult();

        saml2Configuration.SingleSignOnDestination = entityDescriptor.IdPSsoDescriptor.SingleSignOnServices.First().Location;
        foreach (var signingCertificate in entityDescriptor.IdPSsoDescriptor.SigningCertificates)
        {
            saml2Configuration.SignatureValidationCertificates.Add(signingCertificate);
        }

        saml2Configuration.SignAuthnRequest = entityDescriptor.IdPSsoDescriptor.WantAuthnRequestsSigned.Value;

        return saml2Configuration;
    });            

    services.AddSaml2();
    services.AddHttpClient();

    services.AddControllersWithViews();
}

And here, I am trying to translate that to a .Net 7 Program.cs like this:

// .NET 7 Program.cs

var builder = WebApplication.CreateBuilder(args);

var configuration = builder.Configuration;

builder.Services.AddRazorPages();

builder.Services.Configure<Saml2Configuration>(configuration.GetSection("Saml2"));

builder.Services.Configure<Saml2Configuration>(saml2Configuration =>
{
    saml2Configuration.AllowedAudienceUris.Add(saml2Configuration.Issuer);

    var entityDescriptor = new EntityDescriptor();
    entityDescriptor.ReadIdPSsoDescriptorFromUrl(new Uri(configuration["Saml2:IdPMetadata"]));
    saml2Configuration.SingleSignOnDestination = entityDescriptor.IdPSsoDescriptor.SingleSignOnServices.First().Location;
    saml2Configuration.SignatureValidationCertificates.AddRange(entityDescriptor.IdPSsoDescriptor.SigningCertificates);

});

builder.Services.AddSaml2();

var app = builder.Build();

However, whenever I run it, I am getting this error:

InvalidOperationException: Unable to resolve service for type 'ITfoxtec.Identity.Saml2.Saml2Configuration' while attempting to activate 'JMUSAMLConnector.Controllers.AuthController'.
Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, bool isDefaultParameterRequired)

My AuthController is trying to use it like this:

using ITfoxtec.Identity.Saml2;
using ITfoxtec.Identity.Saml2.Schemas;
using ITfoxtec.Identity.Saml2.MvcCore;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using JMUSAMLConnector.Identity;
using System.Security.Authentication;

namespace JMUSAMLConnector.Controllers
{
    [AllowAnonymous]
    [Route("Auth")]
    public class AuthController : Controller
    {
        const string relayStateReturnUrl = "ReturnUrl";
        private readonly Saml2Configuration config;

        public AuthController(Saml2Configuration config)
        {
            this.config = config;
        }
    }
}
英文:

I'm trying to translate a .NetCore project to a .Net 7 project.

The project uses the ITFoxTec SAML2 library.

The .NetCore project has a Startup.cs file that looks like this:

// .NetCore Startup.cs

public void ConfigureServices(IServiceCollection services)
{
	services.BindConfig&lt;Saml2Configuration&gt;(Configuration, &quot;Saml2&quot;, (serviceProvider, saml2Configuration) =&gt;
	{
		saml2Configuration.AllowedAudienceUris.Add(saml2Configuration.Issuer);

		var httpClientFactory = serviceProvider.GetService&lt;IHttpClientFactory&gt;();
		var entityDescriptor = new EntityDescriptor();
		entityDescriptor.ReadIdPSsoDescriptorFromUrlAsync(httpClientFactory, new Uri(Configuration[&quot;Saml2:IdPMetadata&quot;])).GetAwaiter().GetResult();

		saml2Configuration.SingleSignOnDestination = entityDescriptor.IdPSsoDescriptor.SingleSignOnServices.First().Location;
		foreach (var signingCertificate in entityDescriptor.IdPSsoDescriptor.SigningCertificates)
		{
			saml2Configuration.SignatureValidationCertificates.Add(signingCertificate);
		}

		saml2Configuration.SignAuthnRequest = entityDescriptor.IdPSsoDescriptor.WantAuthnRequestsSigned.Value;

		return saml2Configuration;
	});            

	services.AddSaml2();
	services.AddHttpClient();

	services.AddControllersWithViews();
}

And here, I am trying to translate that to a .Net 7 Program.cs like this:

// .NET 7 Program.cs

var builder = WebApplication.CreateBuilder(args);

var configuration = builder.Configuration;

builder.Services.AddRazorPages();

builder.Services.Configure&lt;Saml2Configuration&gt;(configuration.GetSection(&quot;Saml2&quot;));

builder.Services.Configure&lt;Saml2Configuration&gt;(saml2Configuration =&gt;
{
	saml2Configuration.AllowedAudienceUris.Add(saml2Configuration.Issuer);

	var entityDescriptor = new EntityDescriptor();
	entityDescriptor.ReadIdPSsoDescriptorFromUrl(new Uri(configuration[&quot;Saml2:IdPMetadata&quot;]));
	saml2Configuration.SingleSignOnDestination = entityDescriptor.IdPSsoDescriptor.SingleSignOnServices.First().Location;
	saml2Configuration.SignatureValidationCertificates.AddRange(entityDescriptor.IdPSsoDescriptor.SigningCertificates);

});

builder.Services.AddSaml2();

var app = builder.Build();

However, whenever I run it, I am getting this error:

InvalidOperationException: Unable to resolve service for type &#39;ITfoxtec.Identity.Saml2.Saml2Configuration&#39; while attempting to activate &#39;JMUSAMLConnector.Controllers.AuthController&#39;.
Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, bool isDefaultParameterRequired)

My AuthController is trying to use it like this:

using ITfoxtec.Identity.Saml2;
using ITfoxtec.Identity.Saml2.Schemas;
using ITfoxtec.Identity.Saml2.MvcCore;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using JMUSAMLConnector.Identity;
using System.Security.Authentication;

namespace JMUSAMLConnector.Controllers
{
	[AllowAnonymous]
	[Route(&quot;Auth&quot;)]
	public class AuthController : Controller
	{
		const string relayStateReturnUrl = &quot;ReturnUrl&quot;;
		private readonly Saml2Configuration config;

		public AuthController(Saml2Configuration config)
		{
			this.config = config;
		}

答案1

得分: 2

尝试更改此部分:

private readonly Saml2Configuration _config;

public AuthController(IOptions<Saml2Configuration> options)
{
   _config = options.Value;
}
英文:

try changing this:

private readonly Saml2Configuration config;

public AuthController(Saml2Configuration config)
{
   this.config = config;
}

to :

private readonly Saml2Configuration _config;

public AuthController(IOptions&lt;Saml2Configuration&gt; options)
{
   _config = options.Value;
}

huangapple
  • 本文由 发表于 2023年6月29日 00:27:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76575081.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定