ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C# 公众号网页开发 -绑定测试界面

2022-09-14 09:04:48  阅读:311  来源: 互联网

标签:openid code 网页 string C# 绑定 公众 appid


1、微信公众号网页开发授权文档 

 

2、主要测试代码

前端界面代码

<form method="POST" id="submitForm">
    <label for="name">工号</label>
    <input type="text" class="form-control" name="userNameOrEmailAddress" id="userNameOrEmailAddress" placeholder="请输入工号" required="required">
    <label for="name">密码</label>
    <input type="password" class="form-control" name="password" id="password" placeholder="请输入密码" required="required">  
    <label for="name">身份证号码</label>
    <input type="text" class="form-control" name="IDCard" id="IDCard" placeholder="请输入身份证号码" required="required">
    <input hidden name="openid" id="openid" value="@Model.openid"/>
    <button type="submit" class="btn btn-primary" style="margin-top:5px">提交绑定</button> 
</form>

后端代码

/// <summary>
/// 用户公众号openid
/// </summary>
public string openid = "";

/// <summary>
/// 进入界面获取微信code,拿到用户openid信息
/// </summary>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public async Task OnGet()
{//先检查是否已经绑定了openid,进入就获取openid
    string code = Request.Query["code"].ToString();
    string state = Request.Query["state"].ToString();
    if (string.IsNullOrEmpty(code))
    {
        throw new Exception("获取code失败!");
    }
    string appid = "填写公众号appid"; //公众号appid
    string secret = "填写公众号secret";//公众号密钥
    //获取access_token
    string url = $@"https://api.weixin.qq.com/sns/oauth2/access_token?appid={appid}&secret={secret}&code={code}&grant_type=authorization_code";
    HttpClient tokenClient = new();
    string responseBody = await tokenClient.GetStringAsync(url);
    AccessTokenPublicDto at = JsonConvert.DeserializeObject<AccessTokenPublicDto>(responseBody);
    string access_token = at.access_token;
    openid = at.openid;
}

public async Task<IActionResult> OnPostAsync(UserLoginBindInfoDto input)
{
    if (ModelState.IsValid && input!=null)
    {
        bindInfo = await _iWeChatPublicBindService.PostPublicBind(input);
    }
    if (bindInfo.state)
    {
        return LocalRedirect($@"/Message?msg={bindInfo.message}");
    }
    else 
    {
        return Redirect($@"/Message?msg={bindInfo.message}");
    }
}    

 

测试主要代码使用 asp mvc core 项目

当前测试绑定界面需求场景:

小程序和公众号没有做绑定,所以在公众号做了个工号绑定操作,通过工号来关联公众号和小程序。

其他相关链接:

绑定公众号和小程序文档

微信开放平台

 

标签:openid,code,网页,string,C#,绑定,公众,appid
来源: https://www.cnblogs.com/xygsj/p/16691777.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有