Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
Donate
Please sign in before you donate.
Scan WeChat QR to Pay
Cancel
Complete
Prompt
Switch to Alipay.
OK
Cancel
1 Star 0 Fork 10

SyncGithub/OAuthApp

forked from uncle wang/OAuthApp
Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
master
Branches (1)
Tags (15)
master
1.9.20
1.9.6
1.9.2
1.8.26
1.8.18
1.8.15
1.7.29
1.7.12
1.7.4
1.6.29
1.6.22
1.6.14
1.3.7
1.3.1
1.0.0
master
Branches (1)
Tags (15)
master
1.9.20
1.9.6
1.9.2
1.8.26
1.8.18
1.8.15
1.7.29
1.7.12
1.7.4
1.6.29
1.6.22
1.6.14
1.3.7
1.3.1
1.0.0
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
master
Branches (1)
Tags (15)
master
1.9.20
1.9.6
1.9.2
1.8.26
1.8.18
1.8.15
1.7.29
1.7.12
1.7.4
1.6.29
1.6.22
1.6.14
1.3.7
1.3.1
1.0.0
OAuthApp
/
Controllers
/
WechatController.cs
OAuthApp
/
Controllers
/
WechatController.cs
WechatController.cs 5.20 KB
Copy Edit Raw Blame History
uncle wang authored 2022年09月06日 16:45 +08:00 . 1,在线编辑改成全屏模式
using OAuthApp.Data;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Linq;
using System.Net.Http;
using System.Web;
namespace OAuthApp.Controllers
{
public class WechatController : Controller
{
readonly HttpContext _httpContext;
readonly AppDbContext _dbContext;
const string Authorize_url = "https://open.weixin.qq.com/connect/oauth2/authorize";
// 开放平台网站应用专用
const string Authorize_url2 = "https://open.weixin.qq.com/connect/qrconnect";
public WechatController(IHttpContextAccessor contextAccessor,
AppDbContext dbContext)
{
_httpContext = contextAccessor.HttpContext;
_dbContext = dbContext;
}
public const string default_scope = "snsapi_userinfo";
[HttpGet]
public ActionResult Authorize([FromQuery] string code, [FromQuery] long state, [FromQuery] string jxwechat)
{
var props = _dbContext.PropertySettings
.Where(x => x.ChannelCode.Equals(ChannelCodes.App) && x.ChannelAppId == state).ToList();
if (props.Count < 1)
{
return NotFound("请先配置应用");
}
var Prop_WechatClientID = props.FirstOrDefault(x => x.Name.Equals(PropKeyConst.WechatClientID));
var Prop_WechatClientSecret = props.FirstOrDefault(x => x.Name.Equals(PropKeyConst.WechatClientSecret));
var Prop_WechatScope = props.FirstOrDefault(x => x.Name.Equals(PropKeyConst.WechatScope));
var Prop_WechatRedirectUri = props.FirstOrDefault(x => x.Name.Equals(PropKeyConst.WechatRedirectUri));
if (Prop_WechatClientID == null ||
Prop_WechatClientSecret == null ||
Prop_WechatScope == null ||
Prop_WechatRedirectUri == null)
{
return NotFound("请先配置应用");
}
var WechatClientID = Prop_WechatClientID.Value;
var WechatClientSecret = Prop_WechatClientSecret.Value;
var WechatScope = Prop_WechatScope.Value;
var WechatRedirectUri = Prop_WechatRedirectUri.Value;
if (string.IsNullOrWhiteSpace(code) && string.IsNullOrWhiteSpace(jxwechat))
{
var host = _httpContext.Request.Scheme + "://" + _httpContext.Request.Host.ToString();
var redirect_uri = $"{host}/Wechat/Authorize";
var queryStrings = $"appid={WechatClientID}&redirect_uri={redirect_uri}&response_type=code&scope={WechatScope}&state={state}#wechat_redirect";
var redirectTo = string.Empty;
if (WechatScope.Equals("snsapi_login"))
{
redirectTo = Authorize_url2 + "?" + queryStrings;
}
else
{
redirectTo = Authorize_url + "?" + queryStrings;
}
return new RedirectResult(redirectTo);
}
else
{
var OAuthorizeResult = GetAccessToken(WechatClientID, WechatClientSecret, code, WechatScope);
var redirectTo = WechatRedirectUri + $"?id={state}&app_wechat={HttpUtility.UrlEncode(OAuthorizeResult)}";
return new RedirectResult(redirectTo);
}
}
private static string GetAccessToken(string client_id, string client_secret, string code, string scope)
{
var result = string.Empty;
using (var hc = new HttpClient())
{
var url = $"https://api.weixin.qq.com/sns/oauth2/access_token?appid={client_id}&secret={client_secret}&code={code}&grant_type=authorization_code";
result = hc.GetStringAsync(url).Result;
}
var responseJson = JsonConvert.DeserializeObject<JObject>(result);
#region 报错
if (responseJson["errcode"] != null && responseJson["errcode"].HasValues)
{
return responseJson["errcode"].Value<string>();
}
#endregion
#region 如果scopesnsapi_userinfo
if (scope.Equals(default_scope))
{
var access_token = responseJson["access_token"].Value<string>();
var openid = responseJson["openid"].Value<string>();
using (var hc = new HttpClient())
{
var url = $"https://api.weixin.qq.com/sns/userinfo?access_token={access_token}&openid={openid}&lang=zh_CN";
result = hc.GetStringAsync(url).Result;
}
responseJson = JsonConvert.DeserializeObject<JObject>(result);
#region 报错
if (responseJson["errcode"] != null && responseJson["errcode"].HasValues)
{
return responseJson["errcode"].Value<string>();
}
#endregion
responseJson.Add("access_token", access_token);
return JsonConvert.SerializeObject(responseJson);
}
#endregion
//scope是snsapi_base
return result;
}
}
}
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

About

H5应用发布工具
Cancel

Releases

No release

Contributors

All

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C#
1
https://gitee.com/sync-github/OAuthApp.git
git@gitee.com:sync-github/OAuthApp.git
sync-github
OAuthApp
OAuthApp
master
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /