【问题标题】:Unable to inject IAntiforgery in the startup class无法在启动类中注入 IAntiforgery
【发布时间】:2020-10-15 23:23:34
【问题描述】:

需要使用 IAntiforgery 进行 csrf 保护。只是试图注入服务。这样做时,我收到“尝试激活 'APi.Startup' 时无法解析类型 'Microsoft.AspNetCore.Antiforgery.IAntiforgery' 的服务”。错误。我在这里发布代码。希望社区能够在这里为我提供帮助。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using APi.Models;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Antiforgery;
using Microsoft.AspNetCore.Razor.TagHelpers;

 

namespace APi
{
    public class Startup
    {
        IAntiforgery _antiforgery;
        public Startup(IConfiguration configuration,IAntiforgery antiforgery)
        {
            Configuration = configuration;
            _antiforgery = antiforgery;
        }

 

        public IConfiguration Configuration { get; }

 

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllersWithViews();
            services.AddTransient<IAntiforgery>();
            services.AddDbContext<EmployeeContext>(options => 
            options.UseSqlServer(Configuration.GetConnectionString("SQLServerConnection")));
        }

 

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }
            app.UseHttpsRedirection();
            app.UseStaticFiles();

 

            app.UseRouting();

 

            app.UseAuthorization();

 

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}

【问题讨论】:

  • IAntiforgery 直到ConfigureServices() 被调用后才能从 DI 获得,因此构造函数无法使用。只有IHostingEnvironmentIConfiguration 可以注入到构造函数中。
  • 您是否阅读了以下内容:docs.microsoft.com/en-us/aspnet/core/security/…
  • @MartinCostello 感谢您的回复。我不知道。因此,如果我要在使用 IAntiforgery 的 Configure 方法中制作中间件,我该怎么做?
  • 你可以在你的中间件的方法上设置IAntiforgery一个方法来处理请求,或者从HttpContext.RequestServices属性中解析它。

标签: c# asp.net-core antiforgerytoken


【解决方案1】:

你不能在启动构造函数中注入IAntiforgery。使用Generic HostIHostBuilder)时,只能将以下服务类型注入Startup构造函数:

  • IWebHostEnvironment
  • IHostEnvironment
  • IConfiguration

Reference

【讨论】:

    猜你喜欢
    • 2014-10-13
    • 2017-09-29
    • 2019-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多