点网核心2.1自定义身份中间件。未指定authenticationScheme,也未找到DefaultForbidScheme

时间:2018-06-26 12:06:11

标签: asp.net-core-2.0

将我们的应用程序从.net core 1.1迁移到2.1

控制器使用Authorize属性:

[Authorize(Roles = "Administrator")]
public class HomeController : Controller
{
    public IActionResult Index()
    {
        return View();
    }
}

在Startup.cs中添加其他中间件(提供的ConfigureServices和Configure方法):

public void ConfigureServices(IServiceCollection services)
{
    services.AddOptions();
    services.Configure<WebSectionConfigModel>(Configuration.GetSection("Web"));
    services.AddDbContext<MessagingContext>(options => options.UseSqlServer(Configuration.GetConnectionString("messagingConnection")), ServiceLifetime.Transient);
    services.AddTransient(/*...*/);
    services.AddSingleton(/*...*/);

    services.AddSignalR(/* ... */);

   services.AddMvc()
       .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

   services.AddAuthentication(options =>
   {
       options.DefaultAuthenticateScheme = "Anonymous";
       options.DefaultForbidScheme = "Anonymous";
   });

   services.Configure<ForwardedHeadersOptions>(/*...*/);

   services.AddAutoMapper();
}


 public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IApplicationLifetime applicationLifetime)
 {
     loggerFactory.AddConsole(LogLevel.Debug);
     loggerFactory.AddDebug();
     loggerFactory.AddNLog();

     app.UseStaticFiles();

     app.UseCors(policyBuilder =>
     {
         /*...*/
     });


     app.UseMiddleware<CustomIdentityMiddleWare>();
     app.UseMiddleware(typeof(ErrorHandlingMiddleware));

     app.UseWebSockets();
     app.UseSignalR(routes =>
     {
         routes.MapHub<MessagesHub>("/messageshub");
     });

     app.UseMvc(Router.GetRouter);
 }

在CustomIdentityMiddleWare.cs代码中,我创建自定义身份:

public class CustomIdentityMiddleWare
{
    private readonly RequestDelegate _next;

    /* ... ctor and other method ... */

    public async Task Invoke(HttpContext context)
    {
        /* ... */
        ClaimsIdentity claimsIdentity = new CustomIdentity(claims, id, "Custom");

        ClaimsPrincipal claimsPrincipal = new ClaimsPrincipal(claimsIdentity);
        context.User = claimsPrincipal;
        await _next(context);

        /* ... */
    }
}

CustomIdentity代码:

public class CustomIdentity : ClaimsIdentity
{
    public Guid Id { get; set; }

    public CustomIdentity(IEnumerable<Claim> claims, Guid Id, string authenticationType) : base(claims, authenticationType)
    {
        this.Id = Id;
    }
}

在.net核心版本1.1中,一切正常。 但是在2.1版本中,我开始收到错误消息:

  

2018-06-26 14:11:05.6088 |错误|消息:没有authenticationScheme   指定,并且没有找到DefaultForbidScheme。 StackTrace:
  在   Microsoft.AspNetCore.Authentication.AuthenticationService.ForbidAsync(HttpContext   上下文,字符串方案,AuthenticationProperties属性)   Microsoft.AspNetCore.Mvc.ForbidResult.ExecuteResultAsync(ActionContext   上下文)   Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeResultAsync(IActionResult   结果)   Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAlwaysRunResultFilters()   在   Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()   在Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
  在Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext   httpContext)   Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext   httpContext)   messages.moex.com_v2.Infrastructure.ErrorHandlingMiddleware.Invoke(HttpContext   上下文)

我知道我应该以某种方式配置AddAuthentication,但不知道具体如何。

尝试过此代码,但没有运气:

services.AddAuthentication(options =>
{
    options.DefaultAuthenticateScheme = "Anonymous";
    options.DefaultForbidScheme = "Anonymous";
});

0 个答案:

没有答案