实现 client 之前启动一下上一节的 server,启动之前需要清除一些代码
public static void Main(string[] args)
{
BuildWebHost(args)
//.MigrateDbContext((context, services) => {
// new ApplicationDbContextSeed().SeedAsync(context, services)
// .Wait();
//})
.Run();
}
[Required]
//[DataType(DataType.EmailAddress)]
//public string Email{get;set;}
public string UserName { get; set; }
启动程序,使用 Config 中的 TestUser 登录
登录成功,不过现在是在本地,接下来需要把它放到客户端里面
新建一个 Asp.Net Core MVC 网站 MvcClient
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.Configure(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.AddAuthentication(options =>
{
options.DefaultScheme = "Cookies";
options.DefaultChallengeScheme = "oidc";
})
.AddCookie("Cookies")
.AddOpenIdConnect("oidc", options =>
{
options.SignInScheme = "Cookies";
options.Authority = "http://localhost:5000";
options.RequireHttpsMetadata = false;
options.ClientId = "client";
options.ClientSecret = "secret";
options.SaveTokens = true;
});
}
app.UseAuthentication();
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseUrls("http://localhost:5001")
.UseStartup();
客户端设置为5001来启动,然后服务端设置为5000
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseEnvironment("Development")
.UseUrls("http://localhost:5000")
.UseStartup()
.Build();
public static IEnumerable GetClients()
{
return new List
{
new Client()
{
ClientId = "client",
AllowedGrantTypes = GrantTypes.Implicit,// 隐式模式
ClientSecrets =
{
new Secret("secret".Sha256())
},
RedirectUris = { "http://localhost:5001/signin-oidc" },
PostLogoutRedirectUris = { "http://localhost:5001/signout-callback-oidc" },
//AllowedScopes = {"api"},
AllowedScopes =
{
IdentityServerConstants.StandardScopes.Profile,
IdentityServerConstants.StandardScopes.OpenId,
}
}
};
}
[Authorize]
public class HomeController : Controller
"applicationUrl": "http://localhost:5001",
"sslPort": 0
启动服务端,客户端,可以看到跳转到登录界面
登录之后会跳转到 http://localhost:5001/
@{
ViewData["Title"] = "About";
}
@ViewData["Title"]
@ViewData["Message"]
@*Use this area to provide additional information.
*@
@foreach (var claim in User.Claims)
{
- @claim.Type
- @claim.Value
}
启动程序,跳转之后,点击 About 进入 About 页面
主要返回了服务端 Config 中配置的信息
public static IEnumerable GetIdentityResources()
{
return new List
{
new IdentityResources.OpenId(),
new IdentityResources.Profile(),
new IdentityResources.Email(),
};
}
http://video.jessetalk.cn/course/explore
ASP.NET Core分布式项目实战(oauth2 + oidc 实现 server部分)--学习笔记
ASP.NET Core分布式项目实战(oauth2与open id connect 对比)--学习笔记
ASP.NET Core分布式项目实战(详解oauth2授权码流程)--学习笔记
ASP.NET Core分布式项目实战(oauth密码模式identity server4实现)--学习笔记
ASP.NET Core分布式项目实战(第三方ClientCredential模式调用)--学习笔记
ASP.NET Core分布式项目实战(客户端集成IdentityServer)--学习笔记
ASP.NET Core分布式项目实战(业务介绍,架构设计,oAuth2,IdentityServer4)--学习笔记
ASP.NET Core分布式项目实战(课程介绍,MVP,瀑布与敏捷)--学习笔记
ASP.NET Core快速入门 -- 学习笔记汇总