微信網頁授權登入(c# Webform)

風靈使發表於2018-09-12

##Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="chrome=1,webkit=1,IE=edge" />
    <meta name="viewport" content="user-scalable=no,width=device-width,initial-scale=1.0,maximum-scale=1.0" />
    <title>微信授權登入</title>
</head>
<body>
</body>
</html>

##Default.aspx.cs

using System;
using Newtonsoft.Json;

public partial class _Default : WeiXinPage
{
    protected override void OnLoad(EventArgs e)
    {
        Response.Write(JsonConvert.SerializeObject(Data));
        base.OnLoad(e);
    }
}

##WeiXinPage.cs

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using Newtonsoft.Json;

/// <summary>
///     WeiXin 的摘要說明
/// </summary>
public abstract class WeiXinPage : Page, IRequiresSessionState
{
    /// <summary>
    ///     是否訪客身份 true:是 false:否
    /// </summary>
    public static bool isGuest
    {
        get { return HttpContext.Current.Session[_user] == null ? true : false; }
    }

    private static string _user
    {
        get { return "$wxuser"; }
    }

    private static string _code
    {
        get { return "$wxcode"; }
    }

    private static string _AbsoluteUri
    {
        get { return HttpContext.Current.Request.Url.AbsoluteUri; }
    }

    /// <summary>
    ///     該路徑剔除微信Query的URL用於頁面重定向獲取最新的使用者資訊
    /// </summary>
    private static string _RawUrl
    {
        get
        {
            var url = HttpContext.Current.Request.Url.AbsoluteUri;
            url = url.Replace(url.Substring(url.IndexOf("code"), url.IndexOf("STATE") + 5 - url.IndexOf("code")), "");
            return url;
        }
    }

    /// <summary>
    ///     微信使用者資訊
    /// </summary>
    public Dictionary<string, object> Data
    {
        get { return getWxUser(); }
    }

    protected sealed override void OnPreInit(EventArgs e)
    {
        if (isGuest)
        {
            getWxUser();
        }
        base.OnPreInit(e);
    }

    /// <summary>
    ///     獲取微信使用者資訊
    /// </summary>
    private static Dictionary<string, object> getWxUser()
    {
        string code = HttpContext.Current.Request.QueryString.Get("code") ?? "",
            url = "",
            ret = "";
        try
        {
            var user = HttpContext.Current.Session[_user];
            if (code.Length == 0 && user == null)
            {
                JumpUrl();
                return null;
            }
            if (user != null)
                return
                    JsonConvert.DeserializeObject<Dictionary<string, object>>(
                        HttpContext.Current.Session[_user].ToString());

            #region /*如果session中不存在user資訊則對接微信介面獲取使用者資訊*/

            /* 根據code獲取access_token */
            url =
                string.Format(
                    "https://api.weixin.qq.com/sns/oauth2/access_token?appid={0}&secret={1}&code={2}&grant_type=authorization_code"
                    , Config.AppId
                    , Config.AppSecret
                    , code);
            ret = HttpService.Get(url);
            var o = JsonConvert.DeserializeObject<Dictionary<string, string>>(ret);

            #region 注意處理微錯誤{"errcode":40163, "errmsg":"code been used"} 以及{"errcode":40029, "errmsg":"invalid code"}

            if (o.ContainsKey("errmsg"))
            {
                if (o["errcode"] == "40163" || o["errcode"] == "40029")
                {
                    HttpContext.Current.Response.Redirect(_RawUrl, false);
                    return null;
                }
                throw new Exception(o["errmsg"]);
            }

            #endregion

            /* 根據access_token和openid獲取使用者資訊 */
            url = string.Format("https://api.weixin.qq.com/sns/userinfo?access_token={0}&openid={1}&lang=zh_CN"
                , o["access_token"]
                , o["openid"]);
            ret = HttpService.Get(url);
            /*儲存使用者到Session*/
            HttpContext.Current.Session[_user] = ret;

            #endregion

            return
                JsonConvert.DeserializeObject<Dictionary<string, object>>(HttpContext.Current.Session[_user].ToString());
        }
        catch
        {
            throw;
        }
    }

    /// <summary>
    ///     跳轉至微信授權登入頁
    /// </summary>
    private static void JumpUrl()
    {
        var url =
            string.Format(
                "https://open.weixin.qq.com/connect/oauth2/authorize?appid={0}&redirect_uri={1}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect"
                , Config.AppId
                , _AbsoluteUri);
        HttpContext.Current.Response.Redirect(url, false);
    }
}

##HttpService.cs

using System;
using System.Collections.Specialized;
using System.Net;
using System.Text;

/// <summary>
///     HttpService 的摘要說明
/// </summary>
public class HttpService
{
    public static string Get(string url)
    {
        using (var c = new WebClient())
        {
            try
            {
                c.Encoding = Encoding.UTF8;
                return c.DownloadString(url);
            }
            catch (Exception)
            {
                throw;
            }
        }
    }


    public static string GetPost(string url, string parameters)
    {
        var ret = "";
        using (var c = new WebClient())
        {
            try
            {
                c.Encoding = Encoding.UTF8;
                var data = Encoding.UTF8.GetBytes(parameters);
                c.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
                c.Headers.Add("ContentLength", parameters.Length.ToString());
                ret = Encoding.UTF8.GetString(c.UploadData(url, "POST", data));
                return ret;
            }
            catch (Exception)
            {
                throw;
            }
        }
    }


    public static string GetPost(string url, NameValueCollection nv)
    {
        using (var c = new WebClient())
        {
            try
            {
                c.Encoding = Encoding.UTF8;
                return Encoding.UTF8.GetString(c.UploadValues(url, "POST", nv));
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}

##Config.cs

using System.Collections.Generic;
using System.Net;
using System.Text;
using System.Web;
using System.Web.SessionState;
using Newtonsoft.Json;

/// <summary>
///     微信公眾號配置檔案
/// </summary>
public class Config : IRequiresSessionState
{
    /// <summary>
    ///     開發者ID
    /// </summary>
    public static string AppId { get; set; }

    /// <summary>
    ///     開發者密碼
    /// </summary>
    public static string AppSecret { get; set; }

    /// <summary>
    ///     APP access_token 7200秒更新一次
    /// </summary>
    public static string access_token
    {
        get
        {
            try
            {
                if (HttpContext.Current.Session["$access_token"] == null)
                {
                    using (var wc = new WebClient())
                    {
                        wc.Encoding = Encoding.UTF8;
                        var ret =
                            wc.DownloadString(
                                string.Format(
                                    "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}",
                                    AppId, AppSecret));
                        var o = JsonConvert.DeserializeObject<Dictionary<string, object>>(ret);
                        if (o.ContainsKey("errmsg")) return o["errmsg"].ToString();
                        HttpContext.Current.Session.Add("$access_token", o["access_token"].ToString());
                        HttpContext.Current.Session.Timeout = 7200;
                        return o["access_token"].ToString();
                    }
                }
                return HttpContext.Current.Session["$access_token"].ToString();
            }
            catch
            {
                throw;
            }
        }
    }
}

##Global.asax

<%@ Application Language="C#" %>

<script RunAt="server">

    private void Application_Start(object sender, EventArgs e)
    {
        // 在應用程式啟動時執行的程式碼
        Config.AppId = "wx*********aaaa";
        Config.AppSecret = "5454564564646546465456";
    }

    private void Application_End(object sender, EventArgs e)
    {
        //  在應用程式關閉時執行的程式碼
    }

    private void Application_Error(object sender, EventArgs e)
    {
        // 在出現未處理的錯誤時執行的程式碼
    }

    private void Session_Start(object sender, EventArgs e)
    {
        // 在新會話啟動時執行的程式碼
    }

    private void Session_End(object sender, EventArgs e)
    {
        // 在會話結束時執行的程式碼。 
        // 注意: 只有在 Web.config 檔案中的 sessionstate 模式設定為
        // InProc 時,才會引發 Session_End 事件。如果會話模式設定為 StateServer
        // 或 SQLServer,則不引發該事件。
    }

</script>

相關文章