Migrated our app from .net core 1.1 to 2.1
Controllers use Authorize
attribute:
[Authorize(Roles = "Administrator")]
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
}
In Startup.cs add additional middleware (ConfigureServices and Configure methods provided):
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);
}
In CustomIdentityMiddleWare.cs code i create Custom identity:
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 code:
public class CustomIdentity : ClaimsIdentity
{
public Guid Id { get; set; }
public CustomIdentity(IEnumerable<Claim> claims, Guid Id, string authenticationType) : base(claims, authenticationType)
{
this.Id = Id;
}
}
In .net core version 1.1 all works fine. But in 2.1 version I started getting an error message:
2018-06-26 14:11:05.6088|ERROR|Message: No authenticationScheme was specified, and there was no DefaultForbidScheme found. StackTrace:
at Microsoft.AspNetCore.Authentication.AuthenticationService.ForbidAsync(HttpContext context, String scheme, AuthenticationProperties properties) at Microsoft.AspNetCore.Mvc.ForbidResult.ExecuteResultAsync(ActionContext context) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeResultAsync(IActionResult result) at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAlwaysRunResultFilters() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync() at Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext) at Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext) at messaging.moex.com_v2.Infrastructure.ErrorHandlingMiddleware.Invoke(HttpContext context)
I understand that i should configure AddAuthentication somehow, but don't know exactly how.
Tried this code, but with no luck:
services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = "Anonymous";
options.DefaultForbidScheme = "Anonymous";
});