'启动'是一种类型,在给定的上下文中无效

时间:2017-11-23 16:33:31

标签: mysql asp.net-web-api asp.net-core jwt

Asp.Net Core 2.0的新功能以及https://medium.com/@lugrugzo/asp-net-core-2-0-webapi-jwt-authentication-with-identity-mysql-3698eeba6ff8篇文章,以完成基于Mysql JWT的身份验证。

尝试运行项目时出现此错误:

在Program.cs中

public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .Build();

我收到此错误:

error CS0119: 'Startup' is a type, which is not valid in the given context
添加一些块之后的

和Startup类: 在Startup.cs

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    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)
    {
        // ===== Add our DbContext ========
        services.AddDbContext<ApplicationDbContext>();

        // ===== Add Identity ========
        services.AddIdentity<IdentityUser, IdentityRole>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();

        // ===== Add Jwt Authentication ========
        JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear(); // => remove default claims
        services
            .AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;

            })
            .AddJwtBearer(cfg =>
            {
                cfg.RequireHttpsMetadata = false;
                cfg.SaveToken = true;
                cfg.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidIssuer = Configuration["JwtIssuer"],
                    ValidAudience = Configuration["JwtIssuer"],
                    IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["JwtKey"])),
                    ClockSkew = TimeSpan.Zero // remove delay of token when expire
                };
            });

        // ===== Add MVC ========
        services.AddMvc();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(
        IApplicationBuilder app, 
        IHostingEnvironment env,
        ApplicationDbContext dbContext
    )
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        // ===== Use Authentication ======
        app.UseAuthentication();
        app.UseMvc();

        // ===== Create tables ======
        dbContext.Database.EnsureCreated();
    }
}

两个文件; Program.cs和Startup.cs属于同一名称空间。

我无法弄清楚究竟是什么代码块造成了问题。我认为这些错误应该经常发生。所以,如果有办法检测它,请告诉我。

1 个答案:

答案 0 :(得分:0)

我有同样的问题 但是当我评论那条线时,我在那里发现了一个奇怪的角色 一旦我删除它并取消注释该行,一切都很好。 字符恰好在WordEntities之前。(我已附上图片)

//identityBuilder.AddEntityFrameworkStores<ٌWordEntities>()
//.AddDefaultTokenProviders();

enter image description here