ASP.NET Core集成微信登录

工具:

Visual Studio 2015 update 3

Asp.Net Core 1.0

1 准备工作

申请微信公众平台接口测试帐号,申请网址:(http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login)。申请接口测试号无需公众帐号,可以直接体验和测试公众平台所有高级接口。

1.1 配置接口信息

1.2 修改网页授权信息

点击“修改”后在弹出页面填入你的网站域名:

2 新建网站项目

2.1 选择ASP.NET Core Web Application 模板

2.2 选择Web 应用程序,并更改身份验证为个人用户账户

3 集成微信登录功能

3.1添加引用

打开project.json文件,添加引用Microsoft.AspNetCore.Authentication.OAuth

3.2 添加代码文件

在项目中新建文件夹,命名为WeChatOAuth,并添加代码文件(本文最后附全部代码)。

3.3 注册微信登录中间件

打开Startup.cs文件,在Configure中添加代码:

app.UseWeChatAuthentication(new WeChatOptions()
{

 AppId = "******",

 AppSecret = "******"

});

注意该代码的插入位置必须在app.UseIdentity()下方。

4 代码

WeChatAppBuilderExtensions.cs:

// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using Microsoft.AspNetCore.Authentication.WeChat;
using Microsoft.Extensions.Options;

namespace Microsoft.AspNetCore.Builder
{
 /// <summary>
 /// Extension methods to add WeChat authentication capabilities to an HTTP application pipeline.
 /// </summary>
 public static class WeChatAppBuilderExtensions
 {
  /// <summary>
  /// Adds the <see cref="WeChatMiddleware"/> middleware to the specified <see cref="IApplicationBuilder"/>, which enables WeChat authentication capabilities.
  /// </summary>
  /// <param name="app">The <see cref="IApplicationBuilder"/> to add the middleware to.</param>
  /// <returns>A reference to this instance after the operation has completed.</returns>
  public static IApplicationBuilder UseWeChatAuthentication(this IApplicationBuilder app)
  {
   if (app == null)
   {
    throw new ArgumentNullException(nameof(app));
   }

   return app.UseMiddleware<WeChatMiddleware>();
  }

  /// <summary>
  /// Adds the <see cref="WeChatMiddleware"/> middleware to the specified <see cref="IApplicationBuilder"/>, which enables WeChat authentication capabilities.
  /// </summary>
  /// <param name="app">The <see cref="IApplicationBuilder"/> to add the middleware to.</param>
  /// <param name="options">A <see cref="WeChatOptions"/> that specifies options for the middleware.</param>
  /// <returns>A reference to this instance after the operation has completed.</returns>
  public static IApplicationBuilder UseWeChatAuthentication(this IApplicationBuilder app, WeChatOptions options)
  {
   if (app == null)
   {
    throw new ArgumentNullException(nameof(app));
   }
   if (options == null)
   {
    throw new ArgumentNullException(nameof(options));
   }

   return app.UseMiddleware<WeChatMiddleware>(Options.Create(options));
  }
 }
}

WeChatDefaults.cs:

// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace Microsoft.AspNetCore.Authentication.WeChat
{
 public static class WeChatDefaults
 {
  public const string AuthenticationScheme = "WeChat";

  public static readonly string AuthorizationEndpoint = "https://open.weixin.qq.com/connect/oauth2/authorize";

  public static readonly string TokenEndpoint = "https://api.weixin.qq.com/sns/oauth2/access_token";

  public static readonly string UserInformationEndpoint = "https://api.weixin.qq.com/sns/userinfo";
 }
}

WeChatHandler.cs

// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using Microsoft.AspNetCore.Authentication.OAuth;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http.Authentication;
using Microsoft.AspNetCore.Http.Extensions;
using Microsoft.Extensions.Primitives;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Security.Claims;
using System.Text;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;

namespace Microsoft.AspNetCore.Authentication.WeChat
{
 internal class WeChatHandler : OAuthHandler<WeChatOptions>
 {
  public WeChatHandler(HttpClient httpClient)
   : base(httpClient)
  {
  }

  protected override async Task<AuthenticateResult> HandleRemoteAuthenticateAsync()
  {
   AuthenticationProperties properties = null;
   var query = Request.Query;

   var error = query["error"];
   if (!StringValues.IsNullOrEmpty(error))
   {
    var failureMessage = new StringBuilder();
    failureMessage.Append(error);
    var errorDescription = query["error_description"];
    if (!StringValues.IsNullOrEmpty(errorDescription))
    {
     failureMessage.Append(";Description=").Append(errorDescription);
    }
    var errorUri = query["error_uri"];
    if (!StringValues.IsNullOrEmpty(errorUri))
    {
     failureMessage.Append(";Uri=").Append(errorUri);
    }

    return AuthenticateResult.Fail(failureMessage.ToString());
   }

   var code = query["code"];
   var state = query["state"];
   var oauthState = query["oauthstate"];

   properties = Options.StateDataFormat.Unprotect(oauthState);

   if (state != Options.StateAddition || properties == null)
   {
    return AuthenticateResult.Fail("The oauth state was missing or invalid.");
   }

   // OAuth2 10.12 CSRF
   if (!ValidateCorrelationId(properties))
   {
    return AuthenticateResult.Fail("Correlation failed.");
   }

   if (StringValues.IsNullOrEmpty(code))
   {
    return AuthenticateResult.Fail("Code was not found.");
   }

   //获取tokens
   var tokens = await ExchangeCodeAsync(code, BuildRedirectUri(Options.CallbackPath));

   var identity = new ClaimsIdentity(Options.ClaimsIssuer);

   AuthenticationTicket ticket = null;

   if (Options.WeChatScope == Options.InfoScope)
   {
    //获取用户信息
    ticket = await CreateTicketAsync(identity, properties, tokens);
   }
   else
   {
    //不获取信息,只使用openid
    identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, tokens.TokenType, ClaimValueTypes.String, Options.ClaimsIssuer));
    ticket = new AuthenticationTicket(new ClaimsPrincipal(identity), properties, Options.AuthenticationScheme);
   }

   if (ticket != null)
   {
    return AuthenticateResult.Success(ticket);
   }
   else
   {
    return AuthenticateResult.Fail("Failed to retrieve user information from remote server.");
   }
  }

  /// <summary>
  /// OAuth第一步,获取code
  /// </summary>
  /// <param name="properties"></param>
  /// <param name="redirectUri"></param>
  /// <returns></returns>
  protected override string BuildChallengeUrl(AuthenticationProperties properties, string redirectUri)
  {
   //加密OAuth状态
   var oauthstate = Options.StateDataFormat.Protect(properties);

   //
   redirectUri = $"{redirectUri}?{nameof(oauthstate)}={oauthstate}";

   var queryBuilder = new QueryBuilder()
   {
    { "appid", Options.ClientId },
    { "redirect_uri", redirectUri },
    { "response_type", "code" },
    { "scope", Options.WeChatScope },
    { "state", Options.StateAddition },
   };
   return Options.AuthorizationEndpoint + queryBuilder.ToString();
  }

  /// <summary>
  /// OAuth第二步,获取token
  /// </summary>
  /// <param name="code"></param>
  /// <param name="redirectUri"></param>
  /// <returns></returns>
  protected override async Task<OAuthTokenResponse> ExchangeCodeAsync(string code, string redirectUri)
  {
   var tokenRequestParameters = new Dictionary<string, string>()
   {
    { "appid", Options.ClientId },
    { "secret", Options.ClientSecret },
    { "code", code },
    { "grant_type", "authorization_code" },
   };

   var requestContent = new FormUrlEncodedContent(tokenRequestParameters);

   var requestMessage = new HttpRequestMessage(HttpMethod.Post, Options.TokenEndpoint);
   requestMessage.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
   requestMessage.Content = requestContent;
   var response = await Backchannel.SendAsync(requestMessage, Context.RequestAborted);
   if (response.IsSuccessStatusCode)
   {
    var payload = JObject.Parse(await response.Content.ReadAsStringAsync());

    string ErrCode = payload.Value<string>("errcode");
    string ErrMsg = payload.Value<string>("errmsg");

    if (!string.IsNullOrEmpty(ErrCode) | !string.IsNullOrEmpty(ErrMsg))
    {
     return OAuthTokenResponse.Failed(new Exception($"ErrCode:{ErrCode},ErrMsg:{ErrMsg}"));
    }

    var tokens = OAuthTokenResponse.Success(payload);

    //借用TokenType属性保存openid
    tokens.TokenType = payload.Value<string>("openid");

    return tokens;
   }
   else
   {
    var error = "OAuth token endpoint failure";
    return OAuthTokenResponse.Failed(new Exception(error));
   }
  }

  /// <summary>
  /// OAuth第四步,获取用户信息
  /// </summary>
  /// <param name="identity"></param>
  /// <param name="properties"></param>
  /// <param name="tokens"></param>
  /// <returns></returns>
  protected override async Task<AuthenticationTicket> CreateTicketAsync(ClaimsIdentity identity, AuthenticationProperties properties, OAuthTokenResponse tokens)
  {
   var queryBuilder = new QueryBuilder()
   {
    { "access_token", tokens.AccessToken },
    { "openid", tokens.TokenType },//在第二步中,openid被存入TokenType属性
    { "lang", "zh_CN" }
   };

   var infoRequest = Options.UserInformationEndpoint + queryBuilder.ToString();

   var response = await Backchannel.GetAsync(infoRequest, Context.RequestAborted);
   if (!response.IsSuccessStatusCode)
   {
    throw new HttpRequestException($"Failed to retrieve WeChat user information ({response.StatusCode}) Please check if the authentication information is correct and the corresponding WeChat Graph API is enabled.");
   }

   var user = JObject.Parse(await response.Content.ReadAsStringAsync());
   var ticket = new AuthenticationTicket(new ClaimsPrincipal(identity), properties, Options.AuthenticationScheme);
   var context = new OAuthCreatingTicketContext(ticket, Context, Options, Backchannel, tokens, user);

   var identifier = user.Value<string>("openid");
   if (!string.IsNullOrEmpty(identifier))
   {
    identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, identifier, ClaimValueTypes.String, Options.ClaimsIssuer));
   }

   var nickname = user.Value<string>("nickname");
   if (!string.IsNullOrEmpty(nickname))
   {
    identity.AddClaim(new Claim(ClaimTypes.Name, nickname, ClaimValueTypes.String, Options.ClaimsIssuer));
   }

   var sex = user.Value<string>("sex");
   if (!string.IsNullOrEmpty(sex))
   {
    identity.AddClaim(new Claim("urn:WeChat:sex", sex, ClaimValueTypes.String, Options.ClaimsIssuer));
   }

   var country = user.Value<string>("country");
   if (!string.IsNullOrEmpty(country))
   {
    identity.AddClaim(new Claim(ClaimTypes.Country, country, ClaimValueTypes.String, Options.ClaimsIssuer));
   }

   var province = user.Value<string>("province");
   if (!string.IsNullOrEmpty(province))
   {
    identity.AddClaim(new Claim(ClaimTypes.StateOrProvince, province, ClaimValueTypes.String, Options.ClaimsIssuer));
   }

   var city = user.Value<string>("city");
   if (!string.IsNullOrEmpty(city))
   {
    identity.AddClaim(new Claim("urn:WeChat:city", city, ClaimValueTypes.String, Options.ClaimsIssuer));
   }

   var headimgurl = user.Value<string>("headimgurl");
   if (!string.IsNullOrEmpty(headimgurl))
   {
    identity.AddClaim(new Claim("urn:WeChat:headimgurl", headimgurl, ClaimValueTypes.String, Options.ClaimsIssuer));
   }

   var unionid = user.Value<string>("unionid");
   if (!string.IsNullOrEmpty(unionid))
   {
    identity.AddClaim(new Claim("urn:WeChat:unionid", unionid, ClaimValueTypes.String, Options.ClaimsIssuer));
   }

   await Options.Events.CreatingTicket(context);
   return context.Ticket;
  }
 }
}

WeChatMiddleware.cs

// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Globalization;
using System.Text.Encodings.Web;
using Microsoft.AspNetCore.Authentication.OAuth;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace Microsoft.AspNetCore.Authentication.WeChat
{
 /// <summary>
 /// An ASP.NET Core middleware for authenticating users using WeChat.
 /// </summary>
 public class WeChatMiddleware : OAuthMiddleware<WeChatOptions>
 {
  /// <summary>
  /// Initializes a new <see cref="WeChatMiddleware"/>.
  /// </summary>
  /// <param name="next">The next middleware in the HTTP pipeline to invoke.</param>
  /// <param name="dataProtectionProvider"></param>
  /// <param name="loggerFactory"></param>
  /// <param name="encoder"></param>
  /// <param name="sharedOptions"></param>
  /// <param name="options">Configuration options for the middleware.</param>
  public WeChatMiddleware(
   RequestDelegate next,
   IDataProtectionProvider dataProtectionProvider,
   ILoggerFactory loggerFactory,
   UrlEncoder encoder,
   IOptions<SharedAuthenticationOptions> sharedOptions,
   IOptions<WeChatOptions> options)
   : base(next, dataProtectionProvider, loggerFactory, encoder, sharedOptions, options)
  {
   if (next == null)
   {
    throw new ArgumentNullException(nameof(next));
   }

   if (dataProtectionProvider == null)
   {
    throw new ArgumentNullException(nameof(dataProtectionProvider));
   }

   if (loggerFactory == null)
   {
    throw new ArgumentNullException(nameof(loggerFactory));
   }

   if (encoder == null)
   {
    throw new ArgumentNullException(nameof(encoder));
   }

   if (sharedOptions == null)
   {
    throw new ArgumentNullException(nameof(sharedOptions));
   }

   if (options == null)
   {
    throw new ArgumentNullException(nameof(options));
   }

   if (string.IsNullOrEmpty(Options.AppId))
   {
    throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, nameof(Options.AppId)));
   }

   if (string.IsNullOrEmpty(Options.AppSecret))
   {
    throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, nameof(Options.AppSecret)));
   }
  }

  /// <summary>
  /// Provides the <see cref="AuthenticationHandler{T}"/> object for processing authentication-related requests.
  /// </summary>
  /// <returns>An <see cref="AuthenticationHandler{T}"/> configured with the <see cref="WeChatOptions"/> supplied to the constructor.</returns>
  protected override AuthenticationHandler<WeChatOptions> CreateHandler()
  {
   return new WeChatHandler(Backchannel);
  }
 }
}

WeChatOptions.cs

// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections.Generic;
using Microsoft.AspNetCore.Authentication.WeChat;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;

namespace Microsoft.AspNetCore.Builder
{
 /// <summary>
 /// Configuration options for <see cref="WeChatMiddleware"/>.
 /// </summary>
 public class WeChatOptions : OAuthOptions
 {
  /// <summary>
  /// Initializes a new <see cref="WeChatOptions"/>.
  /// </summary>
  public WeChatOptions()
  {
   AuthenticationScheme = WeChatDefaults.AuthenticationScheme;
   DisplayName = AuthenticationScheme;
   CallbackPath = new PathString("/signin-wechat");
   StateAddition = "#wechat_redirect";
   AuthorizationEndpoint = WeChatDefaults.AuthorizationEndpoint;
   TokenEndpoint = WeChatDefaults.TokenEndpoint;
   UserInformationEndpoint = WeChatDefaults.UserInformationEndpoint;
   //SaveTokens = true;   

   //BaseScope (不弹出授权页面,直接跳转,只能获取用户openid),
   //InfoScope (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且,即使在未关注的情况下,只要用户授权,也能获取其信息)
   WeChatScope = InfoScope;
  }

  // WeChat uses a non-standard term for this field.
  /// <summary>
  /// Gets or sets the WeChat-assigned appId.
  /// </summary>
  public string AppId
  {
   get { return ClientId; }
   set { ClientId = value; }
  }

  // WeChat uses a non-standard term for this field.
  /// <summary>
  /// Gets or sets the WeChat-assigned app secret.
  /// </summary>
  public string AppSecret
  {
   get { return ClientSecret; }
   set { ClientSecret = value; }
  }

  public string StateAddition { get; set; }
  public string WeChatScope { get; set; }

  public string BaseScope = "snsapi_base";

  public string InfoScope = "snsapi_userinfo";
 }
}

本文已被整理到了《ASP.NET微信开发教程汇总》,欢迎大家学习阅读。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • 云服务器下搭建ASP.NET Core环境

    最近.net core如火如荼,国内这方面环境搭建方面的文档也非常多,但是不少已经是过时的,就算按照那个流程走下去也避免不了一些地方早就不一样了.所以下面我将从头到尾的教大家搭建一次环境,并且成功运行官网的demo. 一.系统环境 本次笔者因为懒的去做虚拟机,所以注册了一个云提供商的试用账户作为本次的主机. 系统: Ubuntu Server 14.04.2 LTS 64bit Mono: 1.0.0-rc1-update1 Coreclr: 1.0.0-rc1-update1 二.正文 1.首

  • ASP.NET Core 1.0 部署 HTTPS(.NET Core 1.0)

    最近要做一个项目,正逢ASP.Net Core 1.0版本的正式发布.由于现代互联网的安全要求,HTTPS加密通讯已成主流,所以就有了这个方案. 本方案启发于一个旧版的解决方案: ASP.NET Core 1.0 部署 HTTPS (.NET Framework 4.5.1) http://www.cnblogs.com/qin-nz/p/aspnetcore-using-https-on-dnx451.html?utm_source=tuicool&utm_medium=referral  在

  • ASP.NET Core 1.0实现邮件发送功能

    准备将一些项目迁移到 asp.net core 先从封装类库入手,在遇到邮件发送类时发现在 asp.net core 1.0中并示提供SMTP相关类库,于是网上一搜发现了MailKit 好东西一定要试一下,何况是开源,下面是代码可实现SMTP邮件发送: using MailKit.Net.Smtp; using MailKit.Security; using MimeKit; using System.Threading.Tasks; namespace ConsoleApp1 { public

  • VS2015 搭建Asp.net core开发环境的方法

    前言 随着ASP.NET Core 1.0 rtm的发布,网上有许多相关.net core 相关文章,最近正好有时间就尝试VS2015 搭建Asp.net core开发环境,以下是简单的搭建过程,下面来一起看看吧. 步骤如下 一.首先你得装个vs2015 并且保证已经升级至 update3及以上(此处附上一个vs2015带up3的下载链接: ed2k://|file|cn_visual_studio_enterprise_2015_with_update_3_x86_x64_dvd_892329

  • Linux(Ubuntu)下搭建ASP.NET Core环境

    今天来学习一下ASP.NET Core 运行在Ubuntu中.无需安装mono . 环境 Ubuntu 14.04.4 LTS 服务器版 全新安装系统. 下载地址:http://mirrors.neusoft.edu.cn/ubuntu-releases/14.04.4/ubuntu-14.04.4-server-amd64.iso 你也可以下载桌面版安装. 下载地址:http://mirrors.neusoft.edu.cn/ubuntu-releases/14.04.4/ 安装DNVM 首先

  • win10下ASP.NET Core部署环境搭建步骤

    随着ASP.NET Core 1.0 rtm的发布,网上有许多相关.net core 相关文章,今刚好有时间也在win10环境上搭建下 ASP.NET Core的部署环境,把过程记录下给大家. 1. 开发运行环境 1> Visual Studio 2015 Update 3* 2> .NET Core 1.0 for Visual Studio (包括asp.net core 模板,其中如果机器上没有.net core sdk会默认安装)地址https://go.microsoft.com/f

  • asp.net core实现文件上传功能

    本文实例为大家分享了单文件上传.多文件上传的功能,供大家参考,具体内容如下 单文件上传  上传文件在Web应用程序中是一个常见的功能.在asp.net core中上传文件并保存在服务器上,是很容易的.下面就来演示一下怎么样在 ASP.NET Core项目中进行文件上传.  首先,创建一个 asp.net core 项目,然后在Controller文件件添加一个HomeController,然后在 Views 文件夹的 Home 文件夹里添加一个 New.cshtml 视图文件.如下图: 添加一个

  • Windows Server 2012 R2 Standard搭建ASP.NET Core环境图文教程

    前言: 随着ASP.NET Core 1.0的发布,论坛里相关的文章也越来越多,正好有时间在测试环境上搭建 ASP.NET Core的发布环境,把过程中遇到的问题写给大家,以便有用到的朋友需要. 环境: Windows Server 2012 R2 Standard with Update MSDN 链接:ed2k://|file|cn_windows_server_2012_r2_with_update_x64_dvd_6052725.iso|5545705472|121EC13B53882E

  • ASP.NET 5已终结,迎来ASP.NET Core 1.0和.NET Core 1.0

    ASP.NET 在过去的 15 年里是个非常不错的"品牌". ASP.NET 4.6 已经支持在生产环境使用:http://get.asp.net. 但是,命名是新的,完全截取自 ASP.NET 框架 -- "ASP.NET 5",但这并不是个好主意,其中一个原因是:5 > 4.6,这样看起来 ASP.NET 5 比 ASP.NET 4.6 版本号更大,更好,甚至是可以替代 ASP.NET 4.6. 所以修改了名字,选择了一个更好的版本号. 重新引入 ASP.

  • ASP.NET Core集成微信登录

    工具: Visual Studio 2015 update 3 Asp.Net Core 1.0 1 准备工作 申请微信公众平台接口测试帐号,申请网址:(http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login).申请接口测试号无需公众帐号,可以直接体验和测试公众平台所有高级接口. 1.1 配置接口信息 1.2 修改网页授权信息 点击"修改"后在弹出页面填入你的网站域名: 2 新建网站项目 2.1 选择ASP.NET C

  • asp.net core集成JWT的步骤记录

    [什么是JWT] JSON Web Token(JWT)是目前最流行的跨域身份验证解决方案. JWT的官网地址:https://jwt.io/ 通俗地来讲,JWT是能代表用户身份的令牌,可以使用JWT令牌在api接口中校验用户的身份以确认用户是否有访问api的权限. JWT中包含了身份认证必须的参数以及用户自定义的参数,JWT可以使用秘密(使用HMAC算法)或使用RSA或ECDSA的公钥/私钥对进行签名. [什么时候应该使用JSON Web令牌?] 授权:这是使用JWT的最常见方案.一旦用户登录

  • ASP.NET Core 集成 React SPA应用的步骤

    目录 wwwroot\ui ReactUIMiddleware 运行一下 总结 AgileConfig的UI使用react重写快完成了.上次搞定了基于jwt的登录模式(AntDesign Pro + .NET Core 实现基于JWT的登录认证),但是还有点问题.现在使用react重写后,agileconfig成了个确确实实的前后端分离项目.那么其实部署的话要分2个站点部署,把前端build完的静态内容部署在一个网站,把server端也部署在一个站点.然后修改前端的baseURL让spa的api

  • asp.net core 集成swagger ui的原理解析

    什么是Swagger? 说swagger 之前,我们先说一下OpenApi 规范. OpenApi 是一种和语言无关的用于描述RESTAPIs 接口功能的一种规范,对RESTAPIs 接口的描述包括: 接口参数信息.接口返回值信息.api 功能描述.请求路径等. 这里我们说OpenApi 只是一种规范,既然是一种规范,就必然有相应的实现,Swagger 就是其中一个实现了Open Api 规范的工具. .net 中RESTAPIs的代表便是 web api ,并且.net 针对Web Api 也

  • ASP.NET Core集成Apollo(阿波罗)

    目录 1.介绍 2.架构和模块 2.1用户在配置发布后的实时推送设计 2.2Apollo客户端的实现原理 2.3环境配置(Environment) 3.Apollo在Windows上快速启动 3.1准备工作 3.1.1 Java jdk 3.1.2MySQL 3.1.3下载快速启动安装包 3.2安装步骤 3.2.1创建数据库 3.2.2配置数据库连接信息 3.3启动Apollo配置中心 4.ASP.NET Core集成Apollo快速开发 4.1Apollo环境配置 4.2ASP.NET Cor

  • php的laravel框架快速集成微信登录的方法

    本文面向的是php语言laravel框架的用户,介绍的是基于该框架实现的一个简易集成微信登录的方法.使用方法如下: 1. 安装php_weixin_provider 在项目下运行composer require thirdproviders/weixin,即可完成安装.安装成功后,在项目的vendor目录下应该能看到php_weixin_provider的库文件: 2. 配置微信登录的参数 一共有7个参数可以配置,分别是: client_id:对应公众号创建的应用appid client_sec

  • asp.net core集成MongoDB的完整步骤

    一.前言及MongoDB的介绍 最近在整合自己的框架,顺便把MongoDBD的最简单CRUD重构一下作为组件化集成到asp.net core项目中,当然此篇文章中没有讲解mongodb的集群部署,等有机会分享一下. 首先,我们在MongoDB的官方文档中看到,MongoDb的2.4以上的For .Net的驱动是支持.Net Core 2.0的. 针对MongoDB,我想大家应该不陌生,没有用过也有听过. 1.mongodb是什么? MongoDB是一个基于分布式文件存储的数据库,为web应用提供

  • Asp.Net Core 企业微信静默授权的实现

    企业微信接口文档 1. 构造授权网页链接 2.回调获取到 Code 通过code+access_token去 请求用户信息 3. 获取access_token 调试准备工作 -->内网穿透+域名 推荐向日葵有免费的,免费的开发测试够用了 域名的配置成可信用 上代码 Demo下载 [ApiController] [Route("api/[controller]")] public class Auth2Controller : ControllerBase { private re

  • asp.net core集成CKEditor实现图片上传功能的示例代码

    背景 本文为大家分享了asp.net core 如何集成CKEditor ,并实现图片上传功能的具体方法,供大家参考,具体内容如下. 准备工作 1.visual studio 2019 开发环境 2.net core 2.0 及以上版本 实现方法 1.新建asp.net core web项目 2.下载CKEditor 这里我们新建了一个系统自带的样本项目,去 CKEditor官网下载一个版本,解压后拷贝大wwwroot中 3.增加图片上传控制器 @using CompanyName.Projec

  • asp.net core集成kindeditor实现图片上传功能

    本文为大家分享了asp.net core 如何集成kindeditor并实现图片上传功能的具体方法,供大家参考,具体内容如下 准备工作 1.visual studio 2015 update3开发环境 2.net core 1.0.1 及以上版本 目录 新建asp.net core web项目 下载kindeditor 增加图片上传控制器 配置kindeditor参数 代码下载 新建asp.net core web项目 新建一个asp.net core项目,这里命名为kindeditor 选中w

随机推荐