在这个tutorial之后,我在Startup.cs文件中遇到了一个问题:
(需要向下滚动一点,抱歉)
问题出在默认标识上,出现以下错误:
"IServiceCollection不包含AddDefaultIdentity的定义,也没有可访问的扩展方法AddDefaultIdentity接受类型为“IServiceCollection and be found(是否缺少using指令或程序集引用?)”的第一个参数
我查找了documentation,但我遗漏了我所犯的错误,我见过一堆与我类似的案例,但他们的解决方案(包括)似乎不起作用。我可以帮我们一些忙,提前谢谢。
如果你想看一下,“我的”代码是HERE。
发布于 2019-06-04 02:06:15
如果您使用Jwt,您不应该添加标识: AddDefaultIdentity扩展方法用于为autentication...Note页面和MVC添加默认的UI服务。而且它还要求您添加StaticFiles。
另请注意Configure方法中的附加代码及其排列
在您的启动类中尝试以下内容:
public class Startup
{
//add
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().AddNewtonsoftJson();
services.AddTransient<IJwtTokenService, JwtTokenService>();
//Setting up Jwt Authentication
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = Configuration["Jwt:Issuer"],
ValidAudience = Configuration["Jwt:Audience"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Jwt:Key"]))
};
});
services.AddResponseCompression(opts =>
{
opts.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(
new[] { "application/octet-stream" });
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseResponseCompression();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBlazorDebugging();
}
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(routes =>
{
routes.MapDefaultControllerRoute();
});
app.UseBlazor<Client.Startup>();
}
}
}
希望这能帮到你。
更新1:*用上面的代码更新你的启动类*像这样注释你的SampleDataController控制器:
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)]
[Route("api/[controller]")]
public class SampleDataController : Controller
{
// ..
}
响应应包含创建的JwtToken
执行流程摘要:将get请求发布到您的Web Api。路由点WeatherForecasts的请求被重定向到令牌控制器,该控制器的目的是创建一个JWT令牌并将其返回给调用者。
要做的事情:
注意:您可以将Jwt令牌作为cookie从服务器传递给Blazor,其中包含有关用户的更多详细信息。
我们查询我们的IsAutenticated方法。如果用户已通过身份验证,则检索Jwt令牌并将其添加到通过HttpClient调用传递的headers集合中。
还有更多的..。
你看到了吗?
https://stackoverflow.com/questions/56429882
复制相似问题