使用asp.net core Identity

IdentityServer灵活的设计中有一部分是可以将你的用户和他们的数据保存到数据库中的。如果你以一个新的用户数据库开始,那么,asp.net core Identity是一个选择。这个示例演示了如何在IdentityServer中使用asp.net core Ientity.

该示例假设你已经完成了前面的所有示例,这个示例将要用asp.net core Identity 模板创建一个新的项目,新项目会替换掉之前的IdentityServer。而其它的工程则没有影响。

新建一个asp.net core Identity工程

第一步是在你的解决方案中添加一个asp.net core Identity的工程。考虑到大量的代码都来源于asp.net core Identity,所以这里直接使用visual studio的一个模板。你最后得把旧的IdentityServer删除。但是你还得配置一下。

那就从创建一个asp.net core web app开始把:

然后选择MVC模板:

然后在更改身份验证这里选择“个人用户账户”:

最后,当你选择好之后,点击确定。

修改宿主

别忘了把端口调整到5000.

这样才能兼容之间创建好的客户端和API。

添加IdentityServer的包

添加IdentityServer4.AspNetIdentity的nuget包。因为它依赖IdentityServer包,所以会自动的将IdentityServer4添加到项目中来。

Scopes和客户端的配置

虽然是一个新的项目,我们仍然可以将旧项目中的代码粘贴过来用一下。你现在将之前的IdentityServer中的Config类中的代码粘贴到新项目中。

有一个改变的地方是需要禁用一下确认页面的东西,因为我们现在还没有配置关于确认页面的任何东西。所以我们将RequireConsent设置为false:

new Client
{
ClientId = "mvc",
ClientName = "MVC Client",
AllowedGrantTypes = GrantTypes.HybridAndClientCredentials, RequireConsent = false, ClientSecrets =
{
new Secret("secret".Sha256())
}, RedirectUris = { "http://localhost:5002/signin-oidc" },
PostLogoutRedirectUris = { "http://localhost:5002/signout-callback-oidc" }, AllowedScopes =
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
"api1"
},
AllowOfflineAccess = true
}

配置IdentityServer

还是和之前一样,需要在ConfigureServices和Configure这两个Startup中的方法中进行配置。

ConfigureServices

下面的代码显示了工程创建的时候生成的一些样本代码和底部添加的关于IdentityServer的代码。在之前的示例中,AddTestUsers扩展方法用来将用户注册到DI中,但是这会儿我们用AddAspNetIdentity取代了。这个方法需要一个泛型的类型参数,这个类型参数的类型是你asp.net Identity User的类型。

public void ConfigureServices(IServiceCollection services)
{
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders(); // Add application services.
services.AddTransient<IEmailSender, EmailSender>(); services.AddMvc(); // configure identity server with in-memory stores, keys, clients and scopes
services.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddInMemoryPersistedGrants()
.AddInMemoryIdentityResources(Config.GetIdentityResources())
.AddInMemoryApiResources(Config.GetApiResources())
.AddInMemoryClients(Config.GetClients())
.AddAspNetIdentity<ApplicationUser>();
}

需要注意的是关于IdentityServer的逻辑应该写到AddIdentity方法之后。因为其中有一些方法被重写了。

Configure

这里展示了创建工程的时候生成的代码,还有添加了UseIdentityServer。

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
} app.UseStaticFiles(); // app.UseAuthentication(); // not needed, since UseIdentityServer adds the authentication middleware
app.UseIdentityServer(); app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}

创建数据库

现在已经给出了一个新的项目,你需要创建数据库。你可以通过项目目录下的命令行工具来执行dotnet ef database update -c ApplicationDbContext,像这样:

但我一般都会在程序包管理控制台上输入update-database

创建一个用户

接下来,你需要运行应用并将一个用户创建到数据库中。点击注册(Register)按钮:

然后在注册页面上注册一个用户:

现在,你已经拥有了一个用户,你可以登陆了。

在MVC客户端上面登陆

运行MVC客户端应用,然后你可以点击Secure这个链接来进行登陆:

你会被重定向到asp.net Identity的登陆页面上,输入你新创建的那个用户的信息:

然后你会被跳转到确认页面上,然后又迅速的重定向回MVC客户端(因为我们配置了RequireConsetn=false了。)。然后,关于你的user的一些claim回被列出来。

客户端可以代表你的用户来访问api,通过点击Call API using application identity:

下一步

先前的示例项目中有确认页面、错误页面和登出页面,这些缺失的部分你可以直接复制粘贴过来用。一旦你完成了,那个旧的项目就没用了。。然后你需要将RequireConsent改成true。

最后,放上源码:直接点击下载 sample code for this quickstart 吧。

IdentityServer4【QuickStart】之使用asp.net core Identity的更多相关文章

  1. 从零搭建一个IdentityServer——集成Asp.net core Identity

    前面的文章使用Asp.net core 5.0以及IdentityServer4搭建了一个基础的验证服务器,并实现了基于客户端证书的Oauth2.0授权流程,以及通过access token访问被保护 ...

  2. IdentityServer4 中文文档 -14- (快速入门)使用 ASP.NET Core Identity

    IdentityServer4 中文文档 -14- (快速入门)使用 ASP.NET Core Identity 原文:http://docs.identityserver.io/en/release ...

  3. IdentityServer(12)- 使用 ASP.NET Core Identity

    IdentityServer具有非常好的扩展性,其中用户及其数据(包括密码)部分你可以使用任何想要的数据库进行持久化. 如果需要一个新的用户数据库,那么ASP.NET Core Identity是你的 ...

  4. ASP.NET Core Identity Hands On(1)——Identity 初次体验

    ASP.NET Core Identity是用于构建ASP.NET Core Web应用程序的成员资格系统,包括成员资格.登录和用户数据存储 这是来自于 ASP.NET Core Identity 仓 ...

  5. ASP.NET Core Identity Hands On(2)——注册、登录、Claim

    上一篇文章(ASP.NET Core Identity Hands On(1)--Identity 初次体验)中,我们初识了Identity,并且详细分析了AspNetUsers用户存储表,这篇我们将 ...

  6. ASP.NET Core Identity 实战(2)——注册、登录、Claim

    上一篇文章(ASP.NET Core Identity Hands On(1)--Identity 初次体验)中,我们初识了Identity,并且详细分析了AspNetUsers用户存储表,这篇我们将 ...

  7. ASP.NET Core Identity 实战(4)授权过程

    这篇文章我们将一起来学习 Asp.Net Core 中的(注:这样描述不准确,稍后你会明白)授权过程 前情提要 在之前的文章里,我们有提到认证和授权是两个分开的过程,而且认证过程不属于Identity ...

  8. 用一个应用场景理解ASP.NET Core Identity是什么?

    目录 前言 基于声明的认证(Claims-based Authentication) 应用场景一 在ASP.NET Core 中Identity是如何实现的 类ClaimsPrincipal 考察另外 ...

  9. 用例子看ASP.NET Core Identity是什么?

    原文:用例子看ASP.NET Core Identity是什么? 目录 前言 基于声明的认证(Claims-based Authentication) Claim 在ASP.NET Core Iden ...

随机推荐

  1. E - Intervals 贪心

    Chiaki has n intervals and the i-th of them is [li, ri]. She wants to delete some intervals so that ...

  2. zuul超时问题

    转载:http://blog.csdn.net/tianyaleixiaowu/article/details/78772269 是这样的,今天碰到了微服务响应超时问题,而且超时时间特别短,2秒就超时 ...

  3. Android中实现短信发送的一种方式

    SendSmsActivity.java: package com.test.smsmangerdemo.sendsmsactivity; import android.support.v7.app. ...

  4. TensorFlow 学习资料

    感谢几位大神的笔记: https://blog.csdn.net/column/details/13300.html?&page=3 https://blog.csdn.net/u014595 ...

  5. 自然语言处理之jieba分词

    在处理英文文本时,由于英文文本天生自带分词效果,可以直接通过词之间的空格来分词(但是有些人名.地名等需要考虑作为一个整体,比如New York).而对于中文还有其他类似形式的语言,我们需要根据来特殊处 ...

  6. java和maven环境变量设置,Tomcat部署

    Java环境变量设置 Win10我的电脑右击属性,高级系统设置,高级,环境变量设置 新建系统变量JAVA_HOME 和CLASSPATH  变量名:JAVA_HOME 变量值:C:\Program F ...

  7. 【vue】vue +element 搭建项目,$createElement使用

    语法: 它有三个参数 第一个:html标签名 第二个:配置的数据对象 第三个:内容 应用1:自定义弹窗 html: <el-button type="text" @click ...

  8. 07 Python初学(元组)

    tuple:    元组被称为只读列表,即数据可以被查询,但不能被修改,列表的切片操作同样适用于元组. 元组写在小括号()里,元素之间用逗号隔开 虽然 tuple 的元素不可改变,但他可以包含可变的对 ...

  9. hyperledger中文文档学习-1-词汇表

    参考:https://hyperledgercn.github.io/hyperledgerDocs/glossary/ Chaincode - 链码,即智能合约 链码是一个运行在账本上的软件,它可以 ...

  10. Ubuntu 14.04 LTS 安装 NVIDIA 显卡驱动后的屏幕亮度调节问题

    安装 Ubuntu,对于 NVIDIA 显卡,默认情况下会使用第三方开源驱动,并且一般情况下,第三方开源驱动和系统兼容性更好.由于 NVIDIA 显卡驱动不是开放的,所以对 Linux 系统的原生支持 ...