首页 > PHP资讯 > 工具库 > ASP.NETCore集成微信登录的实例图解

ASP.NETCore集成微信登录的实例图解

工具库
这篇文章主要介绍了ASP.NET Core集成微信登录的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

工具:

Visual Studio 2015 update 3

Asp.Net Core 1.0

1 准备工作

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

1.1 配置接口信息

3.3 注册微信登录中间件

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

app.UseWeChatAuthentication(new WeChatOptions(){ AppId = "******", AppSecret = "******"});

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

4 代码

// 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{ ///  /// Extension methods to add WeChat authentication capabilities to an HTTP application pipeline. ///  public static class WeChatAppBuilderExtensions {  ///   /// Adds the  middleware to the specified , which enables WeChat authentication capabilities.  ///   /// 
  public WeChatMiddleware(   RequestDelegate next,   IDataProtectionProvider dataProtectionProvider,   ILoggerFactory loggerFactory,   UrlEncoder encoder,   IOptions sharedOptions,   IOptions 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)));   }  }  ///   /// Provides the  object for processing authentication-related requests.  ///   /// An  configured with the  supplied to the constructor.  protected override AuthenticationHandler 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{ ///  /// Configuration options for . ///  public class WeChatOptions : OAuthOptions {  ///   /// Initializes a new .  ///   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.  ///   /// Gets or sets the WeChat-assigned appId.  ///   public string AppId  {   get { return ClientId; }   set { ClientId = value; }  }  // WeChat uses a non-standard term for this field.  ///   /// Gets or sets the WeChat-assigned app secret.  ///   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 Core集成微信登录的实例图解的详细内容,更多请关注php中文网其它相关文章!

工具库

本文由欣才IT学院整理发布,未经许可,禁止转载。
支持10不支持0