简体   繁体   English

Asp.net核心2.0 Angular模板:添加身份和身份验证

[英]Asp.net core 2.0 Angular template: add identity and authentication

I'm starting a new project and I will make it using Visual Studio 2017 and that Asp.net Core 2.0 Angular Template. 我正在开始一个新项目,我将使用Visual Studio 2017和Asp.net Core 2.0角度模板。 In this project I need to have authentication with roles. 在这个项目中,我需要对角色进行身份验证。 Here is where I'm getting lost since I've been not working with asp.net for some years. 这是我迷失的地方,因为我已经多年没有使用asp.net了。

My question is: how to add authentication, via JWT, to this Asp.net Core 2.0 angular template? 我的问题是:如何通过JWT向这个Asp.net Core 2.0角度模板添加身份验证? I don't want to use an external authentication service like Auth0. 我不想使用像Auth0这样的外部认证服务。 I want to use my own users. 我想使用自己的用户。 I saw that it can be done using Identity server 4, but so far every tutorial I find is confusing to me. 我看到它可以使用Identity server 4完成,但到目前为止我发现的每个教程都让我很困惑。

What are the steps I should follow to implement these requirements? 我应该遵循哪些步骤来实现这些要求? The answer could be just the name of the features I have to implement. 答案可能只是我必须实现的功能的名称。 I can google and learn how to implement. 我可以谷歌并学习如何实施。 I just need the names 我只需要这些名字

Thanks for any help 谢谢你的帮助

If you are using EntityFramework or EntityFrameworkCore and also you are using entity framework migration, you can easily inherit your DbContext class from IdentityDbContext . 如果您正在使用EntityFramework或EntityFrameworkCore,并且您正在使用实体框架迁移,则可以从IdentityDbContext轻松继承您的DbContext类。 Check below code as an example : 检查下面的代码作为示例:

    using Microsoft.AspNetCore.Identity;
    using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
    using Microsoft.EntityFrameworkCore;
    using Phenomenal.WebApp.Domain.BlogModels;

    namespace Phenomenal.WebApp
    {
        public class PhenomenalContext : IdentityDbContext<IdentityUser>
        {
           public PhenomenalContext(DbContextOptions<PhenomenalContext> options)
               :base(options)
           {

        }

        public DbSet<BlogPost> BlogPosts { get; set; }
        public DbSet<Comment> Comments { get; set; }



    }
}

Once you saved your DbContext you can run below commands in "Package Manager Console" to add Identity Schema and tables to your database : 保存DbContext后,可以在“程序包管理器控制台”中运行以下命令,将标识架构和表添加到数据库中:

1) Add-Migration CreateIdentitySchema 1) Add-Migration CreateIdentitySchema

This will create the migration class. 这将创建迁移类。

2) Update-Database 2) Update-Database

This will update your database and add all the Identity tables. 这将更新您的数据库并添加所有Identity表。

3) Script-Migration in entityframeworkcore or Update-Database -script in entityframework 3)entityframework中的Script-Migration或entityframework中的Update-Database -script

This command will return the SQL queries for creating the schema and tables if you want to create manually. 如果要手动创建,此命令将返回用于创建架构和表的SQL查询。

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM