ASPCore集成微信登录

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

ASP.NETCore集成微信登录这篇文章主要介绍了ASP.NETCore集成微信登录的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下工具:VisualStudio2015update3Asp.NetCore1.01准备工作申请微信公众平台接口测试帐号,申请接口测试号无需公众帐号,可以直接体验和测试公众平台所有高级接口。1.1配置接口信息1.2修改网页授权信息点击“修改”后在弹出页面填入你的网站域名:2新建网站项目2.1选择ASP.NETCoreWebApplication模板2.2选择Web应用程序,并更改身份验证为个人用户账户3集成微信登录功能3.1添加引用打开project.json文件,添加引用Microsoft.AspNetCore.Authentication.OAuth3.2添加代码文件在项目中新建文件夹,命名为WeChatOAuth,并添加代码文件(本文最后附全部代码)。3.3注册微信登录中间件打开Startup.cs文件,在Configure中添加代码:12345678app.UseWeChatAuthentication(newWeChatOptions(){AppId=******,AppSecret=******});注意该代码的插入位置必须在app.UseIdentity()下方。4代码WeChatAppBuilderExtensions.cs:1234567891011121314151617181920212223242526//Copyright(c).NETFoundation.Allrightsreserved.//LicensedundertheApacheLicense,Version2.0.SeeLicense.txtintheprojectrootforlicenseinformation.usingSystem;usingMicrosoft.AspNetCore.Authentication.WeChat;usingMicrosoft.Extensions.Options;namespaceMicrosoft.AspNetCore.Builder{///summary///ExtensionmethodstoaddWeChatauthenticationcapabilitiestoanHTTPapplicationpipeline.////summarypublicstaticclassWeChatAppBuilderExtensions{///summary///Addstheseecref=WeChatMiddleware/middlewaretothespecifiedseecref=IApplicationBuilder/,whichenablesWeChatauthenticationcapabilities.////summary///paramname=appTheseecref=IApplicationBuilder/toaddthemiddlewareto./param///returnsAreferencetothis(thisIApplicationBuilderapp){if(app==null){272829303132333435363738394041424344454647484950thrownewArgumentNullException(nameof(app));}returnapp.UseMiddlewareWeChatMiddleware();}///summary///Addstheseecref=WeChatMiddleware/middlewaretothespecifiedseecref=IApplicationBuilder/,whichenablesWeChatauthenticationcapabilities.////summary///paramname=appTheseecref=IApplicationBuilder/toaddthemiddlewareto./param///paramname=optionsAseecref=WeChatOptions/thatspecifiesoptionsforthemiddleware./param///returnsAreferencetothisinstanceaftertheoperationhascompleted./returnspublicstaticIApplicationBuilderUseWeChatAuthentication(thisIApplicationBuilderapp,WeChatOptionsoptions){if(app==null){thrownewArgumentNullException(nameof(app));}if(options==null){thrownewArgumentNullException(nameof(options));}returnapp.UseMiddlewareWeChatMiddleware(Opti(options));}}}WeChatDefaults.cs:1234567891011121314//Copyright(c).NETFoundation.Allrightsreserved.//LicensedundertheApacheLicense,Version2.0.SeeLicense.txtintheprojectrootforlicenseinformation.namespaceMicrosoft.AspNetCore.Authentication.WeChat{publicstaticclassWeChatDefaults{publicconststringAuthenticationScheme=WeChat;publicstaticreadonlystringAuthorizationEndpoint===}}WeChatHandler.cs12345678910111213141516171819202122232425262728293031323334353637383940//Copyright(c).NETFoundation.Allrightsreserved.//LicensedundertheApacheLicense,Version2.0.SeeLicense.txtintheprojectrootforlicenseinformation.usingMicrosoft.AspNetCore.Authentication.OAuth;usingMicrosoft.AspNetCore.Builder;usingMicrosoft.AspNetCore.Http.Authentication;usingMicrosoft.AspNetCore.Http.Extensions;usingMicrosoft.Extensions.Primitives;usingNewtonsoft.Json.Linq;usingSystem;usingSystem.Collections.Generic;usingSystem.Net.Http;usingSystem.Net.Http.Headers;usingSystem.Security.Claims;usingSystem.Text;usingMicrosoft.AspNetCore.Mvc;usingSystem.Threading.Tasks;namespaceMicrosoft.AspNetCore.Authentication.WeChat{internalclassWeChatHandler:OAuthHandlerWeChatOptions{publicWeChatHandler(HttpClienthttpClient):base(httpClient){}protectedoverrideasyncTaskAuthenticateResultHandleRemoteAuthenticateAsync(){AuthenticationPropertiesproperties=null;varquery=Request.Query;varerror=query[error];if(!StringValues.IsNullOrEmpty(error)){varfailureMessage=newStringBuilder();failureMessage.Append(error);varerrorDescription=query[error_description];if(!StringValues.IsNullOrEmpty(errorDescription)){failureMessage.Append(;Description=).Append(errorDescription);}varerrorUri=query[error_uri];4142434445464748495051525354555657585960616263646566676869707172737475767778798081828384if(!StringValues.IsNullOrEmpty(errorUri)){failureMessage.Append(;Uri=).Append(errorUri);}returnAuthenticateResult.Fail(failureMessage.ToString());}varcode=query[code];varstate=query[state];varoauthState=query[oauthstate];properties=Options.StateDataFormat.Unprotect(oauthState);if(state!=Options.StateAddition||properties==null){returnAuthenticateResult.Fail(Theoauthstatewasmissingorinvalid.);}//OAuth210.12CSRFif(!ValidateCorrelationId(properties)){returnAuthenticateResult.Fail(Correlationfailed.);}if(StringValues.IsNullOrEmpty(code)){returnAuthenticateResult.Fail(Codewasnotfound.);}//获取tokensvartokens=awaitExchangeCodeAsync(code,BuildRedirectUri(Options.CallbackPath));varide

1 / 17
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功