一、接下来建立一个支持 IDP(Identity Provider) 的服务器工程
-
右键 src,新建工程 ---> 选择 ASP.NET Core Web Application
image.png
选择空项目,继续
image.png
项目结构如图
image.png
-
接下来安装 IdentityServer 中间件
nuget package 搜索并安装IdentityServer4
:
image.png
3、新建 Config 静态类,初始化 IDP 使用的内存数据库
public static class Config
{
public static IEnumerable<IdentityResource> GetIdentityResources()
{
return new List<IdentityResource>
{
// 必须, 提供用户名密码等验证
new IdentityResources.OpenId(),
// 添加以支持返回 User 上的自定义属性 Claims, 如 CurrentAddr 等
new IdentityResources.Profile()
};
}
public static IEnumerable<Client> GetClients()
{
return new List<Client>();
}
public static List<TestUser> GetUsers()
{
return new List<TestUser>
{
// 对应到 Images 表初始化数据时用到的两个用户 Claire 和 Frank, SubjectId 取表中的 OwnerId 字段
// Claims 里面的信息可以随自己爱好添加,相当于给 User 增加自定义的属性和值
new TestUser
{
SubjectId = "b7539694-97e7-4dfe-84da-b4256e1ff5c7",
Username = "Frank",
Password = "password",
Claims = new List<Claim>
{
new Claim("name", "Frank Hawk"),
new Claim("website", "https://frank.com"),
new Claim("CurrentAddr","USA. LA")
}
},
new TestUser
{
SubjectId = "b7539694-97e7-4dfe-84da-b4256e1ff5c7",
Username = "Claire",
Password = "pwd123",
Claims = new List<Claim>
{
new Claim("name", "Claire Underwood"),
new Claim("website", "https://claire.com"),
new Claim("CurrentAddr","USA. Tex")
}
},
// 自己的测试用户,SubjectId 用 guid 生成一个
new TestUser
{
SubjectId = "4bc6cfb7-6606-dab2-8bd3-07f779b3fd8c",
Username = "Junguoguo",
Password = "pwd123",
Claims = new List<Claim>
{
new Claim("name", "Alex Mercer"),
new Claim("website", "https://junguoguo.com"),
new Claim("CurrentAddr","CHN. Heaven")
}
}
};
}
}
- 修改 StartUp 类,配置使用 IdentityServer
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddIdentityServer()
.AddDeveloperSigningCredential()
.AddInMemoryIdentityResources(Config.GetIdentityResources())
.AddInMemoryClients(Config.GetClients())
.AddTestUsers(Config.GetUsers());
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseIdentityServer();
}
}
- 运行,地址栏输入:http://localhost:53840/.well-known/openid-configuration
image.png
具体的运行端口号可在Properties
下的launchSettings.json
中配置 - 为程序添加 UI-----打开
github
项目 IdentityServer4.Quickstart.UI
下载其中的文件到Junguoguo.IDP
目录
或者 cmder 命令行运行
curl -L https://raw.githubusercontent.com/IdentityServer/IdentityServer4.Quickstart.UI/release/get.sh | bash
![](https://img.haomeiwen.com/i13169203/a9e8d87ec08470b9.png)
回到 VS 会发现目录结构如下
![](https://img.haomeiwen.com/i13169203/5a539d4d03dd539b.png)
按照
github
上的说明配置 Startup
类
- ConfigureServices 方法内添加
services.AddMvc();
- Configure 方法内添加
app.UseStaticFiles();
和app.UseMvcWithDefaultRoute();
配置完成后启动程序,可以看到页面
image.png
-
使用 https 加密传输
image.png
右键工程属性--> Debug
启用 SSL, 然后复制 SSL 地址到 App URL 中即可。
网友评论