博客
关于我
asp.net core IdentityServer4 实现 implicit(隐式许可)实现第三方登录
阅读量:505 次
发布时间:2019-03-06

本文共 3250 字,大约阅读时间需要 10 分钟。

OAuth 2.0 授权模式及 IdentityServer4 配置指南

前言

OAuth 2.0 提供了四种主要的授权模式(GrantType),其中之一是简化模式(implicit)。该模式的特点是通过浏览器直接向认证服务器申请令牌,无需经过第三方应用程序的服务器处理,省去了“授权码”这一步骤,因此得名。所有操作均在浏览器完成,令牌对访问者是可见的,而客户端无需进行认证。

认证步骤

  • 客户端携带客户端标识及重定向URI到授权服务器。
  • 用户确认是否授予客户端权限。
  • 授权服务器收到许可后,跳转至指定的重定向地址,并附带令牌。
  • 客户端向资源服务器请求资源时,不携带之前获取的令牌片段。
  • 资源服务器向浏览器返回一个脚本。
  • 浏览器根据返回的脚本提取之前获取的令牌。
  • 令牌由浏览器推送给客户端。
  • 配置认证授权服务器

    在 IdentityServer4 中进行配置,确保客户端能够顺利获取令牌。以下是常用配置步骤:

  • 安装 IdentityServer4 NuGet 包。
  • 定义身份资源及可访问API的客户端服务器:
    public class Config{    public static IEnumerable
    GetIdentityResources() { return new List
    { new IdentityResources.OpenId(), new IdentityResources.Profile(), new IdentityResources.Email() }; } public static IEnumerable
    GetClients() { return new List
    { new Client { ClientId = "mvc", ClientName = "MyClient", AllowedGrantTypes = GrantTypes.Implicit, RedirectUris = { "http://localhost:5003/signin-oidc" }, PostLogoutRedirectUris = { "http://localhost:5003/signout-callback-oidc" }, AllowedScopes = new List
    { IdentityServerConstants.StandardScopes.OpenId, IdentityServerConstants.StandardScopes.Profile, IdentityServerConstants.StandardScopes.Email }, RequireConsent = false } }; }}
  • ConfigureServices 方法中注入 IdentityServer4 服务:
    services.AddIdentityServer()    .AddDeveloperSigningCredential()    .AddInMemoryIdentityResources(Config.GetIdentityResources())    .AddInMemoryClients(Config.GetClients())    .AddTestUsers(TestUsers.Users);
  • Configure 方法中添加 IdentityServer4 中间件:
    app.UseIdentityServer();
  • 新建客户端

    在完成上述配置后,新建客户端可以通过以下步骤实现:

  • 安装并配置 IdentityServer4:
    dotnet new -i identityserver4.templates
  • ConfigureServices 方法中添加 GitHub 认证:
    services.AddAuthentication()    .AddCookie("Cookies")    .AddOpenIdConnect("oidc", options =>    {        options.Authority = "http://localhost:5004";        options.RequireHttpsMetadata = false;        options.ClientId = "mvc";        options.SaveTokens = true;        options.GetClaimsFromUserInfoEndpoint = true;    });
  • Configure 方法中添加认证中间件:
    app.UseAuthentication();
  • 运行

    登录成功后,可以通过浏览器获取 ClaimsIdentity,并根据需求进行相应操作。如需进一步配置或问题解决,可参考相关文档或社区资源。

    GitHub 第三方登录配置

    在授权服务器中添加 GitHub 第三方登录,需在 ConfigureServices 方法中进行如下配置:

    public void ConfigureServices(IServiceCollection services){    services.AddMvc();    services.AddIdentityServer()        .AddDeveloperSigningCredential()        .AddInMemoryIdentityResources(Config.GetIdentityResources())        .AddInMemoryClients(Config.GetClients())        .AddTestUsers(TestUsers.Users);    services.AddAuthentication()        .AddGitHub(options =>        {            options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;            options.ClientId = "your client";            options.ClientSecret = "your Secret";        });}

    登录成功后,系统会返回相应的 ClaimsIdentity,用户可根据需求进行处理。

    概要

    以上文档详细介绍了 OAuth 2.0 简化模式(implicit)的工作原理及 IdentityServer4 的配置方法,涵盖了从安装到客户端创建、运行测试以及第三方登录配置等内容,为开发者提供了完整的解决方案。

    转载地址:http://zshbz.baihongyu.com/

    你可能感兴趣的文章
    NLP采用Bert进行简单文本情感分类
    查看>>
    NLP问答系统:使用 Deepset SQUAD 和 SQuAD v2 度量评估
    查看>>
    NLP项目:维基百科文章爬虫和分类【02】 - 语料库转换管道
    查看>>
    NLP:使用 SciKit Learn 的文本矢量化方法
    查看>>
    nmap 使用方法详细介绍
    查看>>
    Nmap扫描教程之Nmap基础知识
    查看>>
    nmap指纹识别要点以及又快又准之方法
    查看>>
    Nmap渗透测试指南之指纹识别与探测、伺机而动
    查看>>
    Nmap端口扫描工具Windows安装和命令大全(非常详细)零基础入门到精通,收藏这篇就够了
    查看>>
    NMAP网络扫描工具的安装与使用
    查看>>
    NMF(非负矩阵分解)
    查看>>
    nmon_x86_64_centos7工具如何使用
    查看>>
    NN&DL4.1 Deep L-layer neural network简介
    查看>>
    NN&DL4.3 Getting your matrix dimensions right
    查看>>
    NN&DL4.7 Parameters vs Hyperparameters
    查看>>
    NN&DL4.8 What does this have to do with the brain?
    查看>>
    nnU-Net 终极指南
    查看>>
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    NO 157 去掉禅道访问地址中的zentao
    查看>>
    no available service ‘default‘ found, please make sure registry config corre seata
    查看>>