Asp.NetCoreWebApi图片上传接口(二)集成IdentityServer4受权访问(附源码)

写在前面

本文地址:http://www.cnblogs.com/yilezhu/p/9315644.html
做者:yilezhu
上一篇关于Asp.Net Core Web Api图片上传的文章使用的是mongoDB进行图片的存储,文章发布后,张队就来了一句,说没有使用GridFS。的确博主只是进行了简单的图片上传以及mongoDB存储操做,目的是提供思路。具体的图片存储,有条件的仍是基于阿里云OSS或者七牛吧,若是实在想用MongDB进行存储的话,建议采用GridFS的方式!
又有人说,GridFS大于16M的时候才适合使用,图片上传已经控制小于1M了,就不必使用GridFS了吧。这里能够指定chunksize的大小。这样性能上就没有什么问题了。并且在性能差很少的时候使用GridFS能够更方便的管理。所以建议若是采用MongDB进行文件存储的话,建议采用GridFS的方式。 这里特别感谢张队的耐心指导!html

为何使用IdentityServer4?

上一篇文章中,给你们讲解了如何经过 Asp.Net Core Web Api实现图片上传的接口,具体的能够[点这里查看][http://www.javashuo.com/article/p-zrjibmdm-n.html] 。这个接口是一个公开的接口,如何发布的话,任何知道调用方法的"任何人"都能任意的调用这个接口,俗称“裸奔”。这时候咱们就应该给接口加入认证以及访问控制机制,来增强安全性!那么咱们怎么来实现接口的认证以及访问控制呢?这时候部分人就会很懵逼了,还有一部分人就会联想到 OpenID Connect 和 OAuth 2.0了!但是怎么实现呢?从到到位搭一个这样的框架,会累死我滴,可能还要通过很长时间的测试呢!别担忧,这时候就体现出Asp.Net Core社区的强大了,咱们的主角IdentityServer4闪亮登场!git

IdentityServer4是什么?能帮咱们作什么呢?

IdentityServer4是一套为 ASP.NET Core 2.0开发的基于OpenID Connect 和 OAuth 2.0 的框架,他能让咱们的系统很轻松的就能不少认证以及受权相关的功能,好比:单点登陆,api访问控制等等!其余的我就不介绍了,社区里面介绍的太多太多了!若是有想了解的OAuth 2.0的能够看看阮一峰的这篇文章[理解OAuth 2.0][http://www.ruanyifeng.com/blog/2014/05/oauth_2_0.html] 。最后 IdentityServer4最最最大好处是开源的,用的人也多,并且比较成熟。想一想是否是有点小激动,火烧眉毛的想试试了。在开始以前,附上[开原地址][https://github.com/IdentityServer/IdentityServer4] 以及[详细文档][https://identityserver4.readthedocs.io/en/release/] 。想了解更多自行阅读官方文档吧!github

为了演示的方便,本文采用的是客户端认证模式,至于其余的几种验证模式,你们能够看下上面给出的阮一峰的文章。还有你们用以前要理解下身份认证服务器(IdentityServer),用户(User),客户端(Client),资源(Resources),身份令牌(Identity Token),访问令牌(Access Token)这些概念。若是不清楚的话能够参考晓晨Master的这篇“ASP.NET Core的身份认证框架IdentityServer4(3)-术语的解释”文章。web

Asp.Net Core Web Api中如何使用IdentityServer4呢?

建立IdentityServer4服务端即“身份认证服务器(IdentityServer)”

  1. 新建一个空的Asp.Net Core Web Api项目,名称为IdentityServer端口为5001,以下图所示json

    IdentityServer

  2. 经过Nuget安装IdentityServer4命令以下,记得程序包管理控制套,上面的项目选择刚刚建立的IdentityServer项目c#

    Install-Package IdentityServer4 

安装IdentityServer4

  1. 这里由于采用OAuth 2.0的客户端模式,因此简单地使用一个类来硬编码一些资源(Resources)

以及客户端(Client),代码以下:api

/// <summary>
    /// yilezhu
    /// 2018.7.15
    /// 由于此处采用in-memory,因此硬编码一些api,以及client
    /// </summary>
    public class ApiConfig
    {
        /// <summary>
        /// 定义ApiResource   这里的资源(Resources)指的就是咱们的API
        /// </summary>
        /// <returns>ApiResource枚举</returns>
        public static IEnumerable<ApiResource> GetApiResources()
        {
            return new[]
            {
                new ApiResource("PictureApi", "图片上传的APi")
            };
        }

        /// <summary>
        /// 定义受信任的客户端 Client
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<Client> GetClients()
        {
            return new[]
            {
                new Client
                {
                    ClientId = "MobileUploadPicture",//客户端的标识,要是唯一的
                    ClientSecrets = new [] { new Secret("yilezhu123".Sha256()) },//客户端密码,进行了加密
                    AllowedGrantTypes = GrantTypes.ClientCredentials,//受权方式,这里采用的是客户端认证模式,只要ClientId,以及ClientSecrets正确便可访问对应的AllowedScopes里面的api资源
                    AllowedScopes = new [] { "PictureApi" }//定义这个客户端能够访问的APi资源数组,上面只有一个api
                }
            };
        }
    }
  1. 在Startup.cs中注入IdentityServer服务并使用中间件,代码以下:数组

    // This method gets called by the runtime. Use this method to add services to the container.
            public void ConfigureServices(IServiceCollection services)
            {
                //注入IdentityServer服务
                services.AddIdentityServer()
                    .AddDeveloperSigningCredential()
                    .AddInMemoryClients(ApiConfig.GetClients())
                    .AddInMemoryApiResources(ApiConfig.GetApiResources());
                services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
            }
    
            // 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();
                app.UseMvc();
            }
  2. 用Postman测试并获取AccessToken吧!以下图所示,在Post请求中传入,认证类型,client_id以及client_secret便可获取AccessToken:安全

成功获取Access_Token

当传入错误的Client_id或者密码将出现下面的结果服务器

错误的密码,获取不到

  1. 至此IdentityServer服务已经简单地完成了!下面改造下咱们的图片上传服务。

改造图片上传接口,加入受权认证

  1. 在图片上传api项目中添加IdentityServer nuget包,这里只须要加入AccessTokenValidation包便可,注意选择api项目:

    Install-Package IdentityServer4.AccessTokenValidation

    安装AccessTokenValidation

  2. appsettings.json中加入IdentityServerOptions,进行IdentityServer的一些配置

"IdentityServerOptions": {

    "ServerIP": "localhost",
    "ServerPort": 5001,
    "IdentityScheme": "Bearer",
    "ResourceName": "PictureApi"
  }
  1. 新建一个类用来匹配这个options,这样能够爽爽的使用:

    /// <summary>
        /// yilezhu
        /// 2018.7.15
        /// IdentityServer的配置选项
        /// </summary>
        public class IdentityServerOptions
        {
            /// <summary>
            /// 受权服务器的Ip地址
            /// </summary>
            public string ServerIP { get; set; }
            /// <summary>
            /// 受权服务器的端口号
            /// </summary>
            public int ServerPort { get; set; }
            /// <summary>
            /// access_token的类型,获取access_token的时候返回参数中的token_type一致
            /// </summary>
            public string IdentityScheme { get; set; }
            /// <summary>
            /// 资源名称,认证服务注册的资源列表名称一致,
            /// </summary>
            public string ResourceName { get; set; }
        }
  2. 在Startup.cs中加入identityServer验证

// This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            //注入Options
            OptionsConfigure(services);
            var identityServerOptions = new IdentityServerOptions();
            Configuration.Bind("IdentityServerOptions", identityServerOptions);
            services.AddAuthentication(identityServerOptions.IdentityScheme)
                .AddIdentityServerAuthentication(options =>
                    {
                        options.RequireHttpsMetadata = false; //是否启用https
                        options.Authority = $"http://{identityServerOptions.ServerIP}:{identityServerOptions.ServerPort}";//配置受权认证的地址
                        options.ApiName = identityServerOptions.ResourceName; //资源名称,跟认证服务中注册的资源列表名称中的apiResource一致
                    }
                );

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
        }

        // 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.UseAuthentication();

            app.UseMvc();
        }

        /// <summary>
        /// yilezhu
        /// 2018.7.10
        /// 注册Options
        /// </summary>
        /// <param name="services">服务容器</param>
        private void OptionsConfigure(IServiceCollection services)
        {
            //MongodbHost信息
            services.Configure<MongodbHostOptions>(Configuration.GetSection("MongodbHost"));
            //图片选项
            services.Configure<PictureOptions>(Configuration.GetSection("PictureOptions"));

        }
  1. 为须要说全访问的图片上传接口添加[Authorize]特性,固然要引用下命名空间:

    using Microsoft.AspNetCore.Authorization;
    /// <summary>
            /// 接口上传图片方法
            /// </summary>
            /// <param name="fileDtos">文件传输对象,传过来的json数据</param>
            /// <returns>上传结果</returns>
            [HttpPost]
            [Authorize]
            public async Task<UploadResult> Post([FromBody] FileDtos fileDtos)
            {
                …………
            }
  2. 把受权服务以及图片上传接口同时启动下,而后Postman再次进行下图片上传的测试:

    多项目启动

    返回结果,未受权

  3. 在请求头上加入咱们获取的token信息,来再次访问下:

    加入验证头

    访问成功

  4. Asp.Net Core Web Api图片上传接口集成Identity Server 4安全认证明例教程到此结束了。

示例代码

[点我下载][https://github.com/yilezhu/ImageUploadApiDemo]

总结

本文经过图片上传这个Asp.Net Core Web Api作引子,而后引入Identity Server 4。而后经过一个简单地实例教程阐述了如何建立Identity Server 以及接口中如何进行受权认证访问。博主尽可能采用通俗易懂的语言进行阐述,步骤也尽可能详细,目的就是为了让初学者也能按照步骤一步一步的实现Identity Server 4的认证。下一篇我会加入SwaggerUI生成接口文档,固然你们也能够看下个人这篇关于SwaggerUI的文章[ASP.NET Core WebApi使用Swagger生成api说明文档看这篇就够了][http://www.javashuo.com/article/p-xlcnpzoq-gy.html] 。这个系列的教程源码,我已经放在github上了,你们能够点这里进行访问源代码。https://github.com/yilezhu/ImageUploadApiDemo

相关文章
相关标签/搜索