Curious-programmer
Curious-programmer

Reputation: 813

Add Authentication to .Net Core 2.0 Angular Template

I am trying to implement authentication with .Net core 2.0 angular template (in visual studio 2017). I have attempted with asp.net Identity trying to follow this tutorial

I am getting stuck right away with adding the dbContext to startUp.cs.

StartUp.cs:

  public void ConfigureServices(IServiceCollection services)
{

    services.AddDbContext<ApplicationDbContext>(options =>
     options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

    services.AddDbContext<EduSmartContext>(options =>
     options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
    services.AddMvc();
}

I get this error In startup.cs when adding applicationDbContext:

the type 'EduSmart.Data.ApplicationDbContext' cannot be used as type parameter 'TContext' in the generic type or method 'EntityFrameworkServiceCollectionExtensions.AddDbContext(IServiceCollection, Action, ServiceLifetime, ServiceLifetime)'. There is no implicit reference conversion from 'EduSmart.Data.ApplicationDbContext' to 'Microsoft.EntityFrameworkCore.DbContext'. EduSmart C:\src\EduSmart\Startup.cs 30 Active

I am looking for one of these three options:

  1. Resolve the error above.
  2. A link to a more clear step by step tutorial to implement authentication with asp.net Identity
  3. Suggest a better way to authenticate Angular Asp.Net core app with a linked resource

Upvotes: 1

Views: 2337

Answers (1)

KavehG
KavehG

Reputation: 323

Without seeing more code, I guess

1.Resolve the error above.

As it states your ApplicationDbContext is not derived from DbContext. And I think moving your EduSmartContext entities to the ApplicationDbContext will do the job for you.

public class EduSmartContext : IdentityDbContext<ApplicationUser>
    {
        public EduSmartContext(DbContextOptions<EduSmartContext> options)
            : base(options)
        {
        }
        public DbSet<Poco1> Poco1s { get; set; }
        ...
        ...

2.A link to a more clear step by step tutorial to implement authentication with asp.net Identity

You are better off searching for token based authentication with Asp.net core webapi. https://logcorner.com/token-based-authentication-using-asp-net-web-api-core/

3.Suggest a better way to authenticate Angular Asp.Net core app with a linked resource

If you're willing to invest on this, I suggest that you use an Authentication Server also known as STS (security Token Server) such as IdentityServer4 or OpenIdDict In case of IdentityServer this articles might be helpful:

https://damienbod.com/2016/10/01/identityserver4-webapi-and-angular2-in-a-single-asp-net-core-project/

Upvotes: 1

Related Questions