What is the difference between UseStaticFiles, UseSpaStaticFiles, and UseSpa in ASP.NET Core 2.1?

Static files, such as HTML, CSS, images, and JavaScript, are assets an ASP.NET Core app serves directly to clients. Some configuration is required to enable serving of these files. UseStaticFiles – Serve files inside of web root (wwwroot folder) UseSpaStaticFiles – Serve static file like image, css, js in asset folder of angular app UseSpa … Read more

Identity in ASP.Net Core 2.1< - Customize AccountController

ASP.NET Core 2.1 introduced new feature called Razor class libraries that lets you build views and pages as part of reusable library. ASP.NET Core Identity was moved to such RCL. You can override it in your project: From Solution Explorer, right-click on the project > Add > New Scaffolded Item. From the left pane of … Read more

ASP.NET Core Identity 2.0 SignoutAsync is not logging out user if the user signed in with Google

The problem is that your RedirectToAction overwrites the redirect to the Identity Server endsession URL that SignOutAsync issues. As for SignOutAsync, what is obsolete is the Authentication portion — as of ASP.NET Core 2.0 it’s an extension directly off HttpContext itself. (The same explanation for the same signout problem is given here by Microsoft’s HaoK.) … Read more

AddJwtBearer OnAuthenticationFailed return custom error

It’s important to note that both the aspnet-contrib OAuth2 validation and the MSFT JWT handler automatically return a WWW-Authenticate response header containing an error code/description when a 401 response is returned: If you think the standard behavior is not convenient enough, you can use the events model to manually handle the challenge. E.g: services.AddAuthentication() .AddJwtBearer(options … Read more

AddDbContext or AddDbContextPool

The answer is here (under “DbContext pooling”): https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-2.0#dbcontext-pooling DbContext is not thread-safe. So you cannot reuse the same DbContext object for multiple queries at the same time (weird things happen). The usual solution for this has been to just create a new DbContext object each time you need one. That’s what AddDbContext does. However, there … Read more

Google JWT Authentication with AspNet Core 2.0

Posting my ultimate approach for posterity. As Tratcher pointed out, the AddGoogle middleware is not actually for a JWT authentication flow. After doing more research, I realized that what I ultimately wanted is what is described here: https://developers.google.com/identity/sign-in/web/backend-auth So my next problems were I could not rely on the standard dotnet core Jwt auth middleware … Read more

The provider for the source IQueryable doesn’t implement IAsyncQueryProvider

I get stuck on this issue today and this lib resolve it for me https://github.com/romantitov/MockQueryable completely, please refer: Mocking Entity Framework Core operations such ToListAsync, FirstOrDefaultAsync etc. //1 – create a List<T> with test items var users = new List<UserEntity>() { new UserEntity{LastName = “ExistLastName”, DateOfBirth = DateTime.Parse(“01/20/2012”)}, … }; //2 – build mock by … Read more

Cascade deleting with EF Core

Cascade delete always works in one direction – from principal entity to dependent entity, i.e. deleting the principal entity deletes the dependent entities. And for one-to- many relationships the one side is always the principal and the many side is the dependent. Looks like you are confused by the fluent configuration. Note that each relationship … Read more

No authenticationScheme was specified, and there was no DefaultChallengeScheme found with default authentification and custom authorization

Do not use authorization instead of authentication. I should get whole access to service all clients with header. The working code is: public class TokenAuthenticationHandler : AuthenticationHandler<TokenAuthenticationOptions> { public IServiceProvider ServiceProvider { get; set; } public TokenAuthenticationHandler (IOptionsMonitor<TokenAuthenticationOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock, IServiceProvider serviceProvider) : base (options, logger, encoder, clock) { ServiceProvider … Read more

instance of entity type cannot be tracked because another instance with same key value is tracked [closed]

Your DB Context is being shared by multiple requests, meaning that the entity you’re editing has been tracked already. This is likely because your repository service is a Singleton rather than Scoped, and so your DB Context is being reused with the entity being tracked when it’s pulled out, and then put back in to … Read more

tech