diff --git a/docs/en/Community-Articles/2022-12-29-Use-Weixin-Authentication-for-MVC-Applications/Post.md b/docs/en/Community-Articles/2022-12-29-Use-Weixin-Authentication-for-MVC-Applications/Post.md new file mode 100644 index 0000000000..f99176eedb --- /dev/null +++ b/docs/en/Community-Articles/2022-12-29-Use-Weixin-Authentication-for-MVC-Applications/Post.md @@ -0,0 +1,68 @@ +# How to Use the Weixin Authentication for MVC / Razor Page Applications + +This guide demonstrates how to integrate Weixin to an ABP application that enables users to sign in using OAuth 2.0 with credentials. + +## Create a sandbox account + +If you don't have a production account, you can [create a sendbox account](https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index) for testing. + +In this article we will use the sandbox account. + +> You should configure the callback domain name on the Weixin open platform + +## AddWeixin + +You need to install `AspNet.Security.OAuth.Weixin` package to your **.Web** project. + +In your **.Web** project, locate your **ApplicationWebModule** and modify `ConfigureAuthentication` method with the following: + +```csharp +private void ConfigureAuthentication(ServiceConfigurationContext context) +{ + var configuration = context.Services.GetConfiguration(); + context.Services.ForwardIdentityAuthenticationForBearer(OpenIddictValidationAspNetCoreDefaults.AuthenticationScheme); + context.Services.AddAuthentication() + .AddWeixin(options => + { + options.ClientId = configuration["Weixin:ClientId"]; + options.ClientSecret = configuration["Weixin:ClientSecret"]; + }); +} +``` + +Updating `appsettings.json` to add `Weixin` section: + +````json + "Weixin": { + "ClientId": "", + "ClientSecret": "" + } +```` + +## Web page authorization + +Now you can run the application to login with Weixin. + +![login-with-weixin](login-with-weixin.jpg) + +It will redirect to weixin platform to scan the QR code. + +> The sandbox account lacks the necessary scope, so it may not work properly. + +## Official account authorization + +Updating `AddWeixin`: + +```csharp +context.Services.AddAuthentication() + .AddWeixin(options => + { + options.ClientId = configuration["Weixin:ClientId"]; + options.ClientSecret = configuration["Weixin:ClientSecret"]; + options.AuthorizationEndpoint = "https://open.weixin.qq.com/connect/oauth2/authorize"; + }); +``` + +Now you can use WeChat app to open the web application URL to login with weixin. + +![offical-account](offical-account.jpg) \ No newline at end of file diff --git a/docs/en/Community-Articles/2022-12-29-Use-Weixin-Authentication-for-MVC-Applications/login-with-weixin.jpg b/docs/en/Community-Articles/2022-12-29-Use-Weixin-Authentication-for-MVC-Applications/login-with-weixin.jpg new file mode 100644 index 0000000000..a2c352f2e2 Binary files /dev/null and b/docs/en/Community-Articles/2022-12-29-Use-Weixin-Authentication-for-MVC-Applications/login-with-weixin.jpg differ diff --git a/docs/en/Community-Articles/2022-12-29-Use-Weixin-Authentication-for-MVC-Applications/offical-account.jpg b/docs/en/Community-Articles/2022-12-29-Use-Weixin-Authentication-for-MVC-Applications/offical-account.jpg new file mode 100644 index 0000000000..28bcdca41e Binary files /dev/null and b/docs/en/Community-Articles/2022-12-29-Use-Weixin-Authentication-for-MVC-Applications/offical-account.jpg differ