ASP.NET Core分布式项目实战(oauth2 + oidc 实现 server部分)--学习笔记

任务15:oauth2 + oidc 实现 server部分

基于之前快速入门的项目(MvcCookieAuthSample):

ASP.NET Core快速入门(第5章:认证与授权)--学习笔记

ASP.NET Core快速入门(第6章:ASP.NET Core MVC)--学习笔记

mvcCookieAuthSample2下载地址:
http://video.jessetalk.cn/course/5/material/217/download

把这个 MVC 注册登录的网站变成一个单点登录,现在它是自己登录自己使用,我们需要把它的登录信息返回给第三方

添加 identityserver4 引用

在 startup 中

using IdentityServer4;
按照之前的文章添加 Config.cs
using System.Collections;
using System.Collections.Generic;
using IdentityServer4.Models;
using IdentityServer4.Test;

namespace mvcCookieAuthSample
{
    public class Config
    {
        public static IEnumerable<Client> GetClients()
        {
            return new List<Client>
            {
                new Client()
                {
                    ClientId = "client",
                    AllowedGrantTypes = GrantTypes.Implicit,// 隐式模式
                    ClientSecrets =
                    {
                        new Secret("secret".Sha256())
                    },
                    AllowedScopes = {"api"},
                }
            };
        }

        public static IEnumerable<ApiResource> GetApiResource()
        {
            return new List<ApiResource>
            {
                new ApiResource("api", "My Api")
            };
        }

        public static IEnumerable<IdentityResource> GetIdentityResources()
        {
            return new List<IdentityResource>
            {
                new IdentityResources.OpenId(),
                new IdentityResources.Profile(),
                new IdentityResources.Email(),
            };
        }

        public static List<TestUser> GetTestUsers()
        {
            return new List<TestUser>
            {
                new TestUser
                {
                    SubjectId = "1",
                    Username = "mingsonzheng",
                    Password = "123456"
                }
            };
        }
    }
}
startup 的 ConfigureServices
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    services.AddIdentityServer()
            .AddDeveloperSigningCredential()
            .AddInMemoryClients(Config.GetClients())
            .AddInMemoryApiResources(Config.GetApiResource())
            .AddInMemoryIdentityResources(Config.GetIdentityResources())
            .AddTestUsers(Config.GetTestUsers());

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

    //services.AddIdentity<ApplicationUser, ApplicationUserRole>()
    //    .AddEntityFrameworkStores<ApplicationDbContext>()
    //    .AddDefaultTokenProviders();

    //services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
    //    .AddCookie(options => {
    //        options.LoginPath = "/Account/Login";
    //    });

    //services.Configure<IdentityOptions>(options =>
    //{
    //    options.Password.RequireLowercase = true;
    //    options.Password.RequireNonAlphanumeric = true;
    //    options.Password.RequireUppercase = true;
    //    options.Password.RequiredLength = 12;
    //});

    services.AddMvc();
}
startup 的 Configure 中 UseIdentityServer
// 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();
    }
    else
    {
        app.UseExceptionHandler("/Home/Error");
    }

    app.UseStaticFiles();
    
    //app.UseAuthentication();
    app.UseIdentityServer();

    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });
}

我们已经把 IdentityServer4 添加到 MVC 程序中,接着需要在 Controller 中实现这个逻辑

首先注释 AccountController 原先的登录逻辑

//private UserManager<ApplicationUser> _userManager;
//private SignInManager<ApplicationUser> _signInManager;

Logout 中使用 HttpContext.SignOutAsync 替换

public async Task<IActionResult> Logout()
{
    //await _signInManager.SignOutAsync();
    await HttpContext.SignOutAsync();
    return RedirectToAction("Index", "Home");
}

接着改造登录的逻辑,我们需要验证用户名和密码,前面我们在 Config 中添加了 TestUser,它被放在 TestUserStore 中,可以通过依赖注入引用进来,有了它之后就可以在登录的时候拿到用户名和密码

private readonly TestUserStore _users;

public AccountController(TestUserStore users)
{
    _users = users;
}

因为 TestUser 本身不提供 Email 登录,所以我们需要修改 LoginViewModel 以及 Login.cshtml

LoginViewModel

[Required]
//[DataType(DataType.EmailAddress)]
//public string Email { get; set; }
public string UserName { get; set; }

Login.cshtml

<div class="form-group">
    <label asp-for="UserName"></label>
    <input asp-for="UserName" class="form-control" />
    <span asp-validation-for="UserName" class="text-danger"></span>
</div>

改造登录的逻辑

public async Task<IActionResult> Login(LoginViewModel loginViewModel,string returnUrl)
{
    if (ModelState.IsValid)
    {
        //ViewData["ReturnUrl"] = returnUrl;
        //var user = await _userManager.FindByEmailAsync(loginViewModel.Email);
        //if (user == null)
        //{
        //    ModelState.AddModelError(nameof(loginViewModel.Email), "Email not exists");
        //}
        //else
        //{
        //    await _signInManager.SignInAsync(user, new AuthenticationProperties { IsPersistent = true });
        //    return RedirectToLoacl(returnUrl);
        //}

        ViewData["ReturnUrl"] = returnUrl;
        var user = _users.FindByUsername(loginViewModel.UserName);
        if (user == null)
        {
            ModelState.AddModelError(nameof(loginViewModel.UserName), "UserName not exists");
        }
        else
        {
            if (_users.ValidateCredentials(loginViewModel.UserName, loginViewModel.Password))
            {
                var props = new AuthenticationProperties
                {
                    IsPersistent = true,
                    ExpiresUtc = DateTimeOffset.UtcNow.Add(TimeSpan.FromMinutes(30)),
                };

                await Microsoft.AspNetCore.Http.AuthenticationManagerExtensions.SignInAsync(
                    HttpContext,
                    user.SubjectId,
                    user.Username,
                    props
                    );

                return RedirectToLoacl(returnUrl);
            }

            ModelState.AddModelError(nameof(loginViewModel.Password), "Wrong Password");
        }
    }

    return View();
}

这样,我们就实现了一个通过 IdentityServer4 下的方法来实现了一个登录逻辑,然后做了一个跳转,下一节再把客户端加进来

课程链接

http://video.jessetalk.cn/course/explore

相关文章

ASP.NET Core分布式项目实战(oauth2与open id connect 对比)--学习笔记

ASP.NET Core分布式项目实战(详解oauth2授权码流程)--学习笔记

ASP.NET Core分布式项目实战(oauth密码模式identity server4实现)--学习笔记

ASP.NET&nbsp;Core分布式项目实战(第三方ClientCredential模式调用)--学习笔记

ASP.NET Core分布式项目实战(客户端集成IdentityServer)--学习笔记

ASP.NET Core分布式项目实战(业务介绍,架构设计,oAuth2,IdentityServer4)--学习笔记

ASP.NET Core分布式项目实战(课程介绍,MVP,瀑布与敏捷)--学习笔记

ASP.NET Core快速入门 -- 学习笔记汇总


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot 2.7及以上版本中,可以使用spring-boot-starter-oauth2-authorization-server-1.2来自定义配置OIDC(OpenID Connect)实例。OIDC是一种基于OAuth 2.0协议的身份验证和授权协议,它允许客户端应用程序通过认证服务器来验证用户的身份并获取访问令牌。 要自定义配置OIDC实例,你可以按照以下步骤进行操作: 1. 首先,在你的Spring Boot项目中添加spring-boot-starter-oauth2-authorization-server-1.2依赖。可以在项目的pom.xml文件中添加以下依赖项: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-oauth2-authorization-server</artifactId> <version>1.2.0</version> </dependency> ``` 2. 接下来,你需要创建一个配置类来定义OIDC实例的配置。可以创建一个类,并使用`@Configuration`注解标记它,然后使用`@EnableAuthorizationServer`注解启用授权服务器功能。在配置类中,你可以使用`@Bean`注解来定义各种组件和配置。 ```java @Configuration @EnableAuthorizationServer public class OIDCConfig extends AuthorizationServerConfigurerAdapter { // 在这里定义你的OIDC实例的配置 } ``` 3. 在OIDCConfig类中,你可以重写`configure(ClientDetailsServiceConfigurer clients)`方法来配置客户端详细信息。可以使用`clients.inMemory()`方法来配置内存中的客户端信息,也可以使用`clients.jdbc(dataSource)`方法来配置数据库中的客户端信息。 ```java @Override public void configure(ClientDetailsServiceConfigurer clients) throws Exception { clients.inMemory() .withClient("client-id") .secret("client-secret") .authorizedGrantTypes("authorization_code", "refresh_token") .scopes("openid") .redirectUris("http://localhost:8080/callback"); } ``` 4. 此外,你还可以根据需要重写其他方法来自定义OIDC实例的配置,例如`configure(AuthorizationServerSecurityConfigurer security)`方法用于配置授权服务器的安全性,`configure(AuthorizationServerEndpointsConfigurer endpoints)`方法用于配置授权服务器的端点等。 至此,你已经完成了自定义配置OIDC实例的步骤。你可以根据具体需求进行更多的配置和定制化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值