微信页面验证

​
 if (!IsPostBack)
        {
            string reurl = "http://www.xxxxxx/webchart/WebApp/MyCertification.aspx";
            //传递参数,获取用户信息后,可跳转到自己定义的页面,想怎么处理就怎么处理
            if (Request.QueryString["state"] != null && Request.QueryString["state"] != "")
            {
               if( Request.QueryString["state"].ToString()=="Un"&& Request.QueryString["code"]!=null&& Request.QueryString["code"].ToString()!="")
                {
                    Log.Info("Authorize is success");
                    code = Request.QueryString["code"].ToString();
                    if (!string.IsNullOrEmpty(code))
                    {
                        WeiXin weixin = new WeiXin();
                        QmsFrameWork.WebChart.WXCodeUser User = weixin.GetUserByCode(code);
                        openid = User.openid;
                        txtopenid.Value = openid;
                        hidopenid.Value = openid;
                        if (User != null && User.openid != null && User.openid != "")
                        {
                            
                            if (QmsFrameWork.Entities.WXUser.ISAuthorization(openid))
                            {
                                Response.Redirect("./MyStaffInfo.aspx?openid="+openid);
                            }
                            else
                            {
                                DataTable dt = QmsFrameWork.Entities.WXUser.QueryDataByOpenID(openid);
                                if (dt == null || dt.Rows.Count == 0)
                                {
                                    weixin.InsertWeiXinUserByOpenID(openid);
                                }
                                else
                                {
                                    string staffid, staffname;
                                 staffid=   txtstaffid.Value = dt.Rows[0]["staff_id"].ToString();
                                 staffname=   txtstaffname.Value = dt.Rows[0]["staff_name"].ToString();

                                    if(!string.IsNullOrEmpty(staffid)&&!string.IsNullOrEmpty(staffname))
                                    {
                                        Response.Redirect("./WaitAuthorization.aspx");
                                    }
                                }
                            }
                            
                        }
                       
                        
                    }

                }
               else
                {
                    Log.Info("Authorize is faile");
                }
            }
            else
            {
                if (Request.QueryString["auth"] != null && Request.QueryString["auth"] != "" && Request.QueryString["auth"] == "1")
                {
                     Response.Redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&redirect_uri=" + reurl + "&response_type=code&scope=snsapi_userinfo&state=Un&connect_redirect=1#wechat_redirect");
                }
                else
                {
                    //不弹出授权页面
                    // Response.Redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&redirect_uri=" + reurl + "&response_type=code&scope=snsapi_base&state=Un&connect_redirect=1#wechat_redirect");
                    Response.Redirect("https://open.weixin.qq.com/connect/oauth2/authorize?appid=" + appid + "&redirect_uri=" + reurl + "&response_type=code&scope=snsapi_userinfo&state=Un&connect_redirect=1#wechat_redirect");


                }
            }
            
            //弹出授权页面(如在不弹出授权页面基础下未获得openid,则弹出授权页面,提示用户授权)
            
        }
​

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要实现微信页面登录,需要进行以下步骤: 1. 在微信开放平台中创建应用并获取AppID和AppSecret。 2. 在Android Studio中创建一个Kotlin项目,并在项目中添加微信开放平台提供的SDK。 3. 在AndroidManifest.xml文件中添加以下代码: ```xml <activity android:name=".wxapi.WXEntryActivity" android:exported="true" android:launchMode="singleInstance" android:taskAffinity="${applicationId}" android:theme="@android:style/Theme.Translucent.NoTitleBar" /> ``` 其中,WXEntryActivity是微信SDK提供的用于接收微信登录结果的Activity。 4. 在项目中创建一个wxapi包,并在该包中创建WXEntryActivity类。该类应继承自WXCallbackActivity,并重写onResp方法,处理微信登录结果。 ```kotlin class WXEntryActivity : WXCallbackActivity() { override fun onResp(resp: BaseResp?) { if (resp is SendAuth.Resp) { val code = resp.code // 获取微信登录授权码 // TODO:将授权码发送到服务器进行登录验证 } super.onResp(resp) } } ``` 5. 在登录页面中添加微信登录按钮,并通过以下代码调用微信登录: ```kotlin val req = SendAuth.Req() req.scope = "snsapi_userinfo" req.state = "wechat_sdk_demo" WXAPIFactory.createWXAPI(this, APP_ID, true).sendReq(req) ``` 其中,scope指定要获取的微信授权作用域,state用于防止CSRF攻击,APP_ID为在微信开放平台中创建应用时获取的AppID。 6. 在服务器端,根据微信授权码进行登录验证,并返回登录结果。 以上就是使用Kotlin实现微信页面登录的基本步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Farmwang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值