zoukankan      html  css  js  c++  java
  • 微信授权封装,欢迎使用

    FastDev.WeiXinHelper

    引用示例:

    基于Mvc网站继续说明:

    1.自定义属性 UserAuthorizeAttribute

    using FastDev.Log;
    using FastDev.Mvc.Extension;
    using FastDev.WeiXinHelper.Auth;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;

    namespace TestWeiXinWap.Controllers.Filter
    {
    /// <summary>
    ///
    /// </summary>
    /// <seealso cref="AuthorizeAttribute" />
    public class UserAuthorizeAttribute : AuthorizeAttribute
    {
    private static string appId = "-------------------------";
    private static string appSecret = "-------------------------";
    private static string authdomain = "http://---------------------------/";
    /// <summary>
    /// 登录筛选器实现
    /// </summary>
    /// <param name="filterContext">filterContext</param>
    public override void OnAuthorization(AuthorizationContext filterContext)
    {
    try
    {
    if (!OAuth2.IsAuth)
    {
    OAuth2.FilterContext = filterContext;
    OAuth2.AppId = appId;
    OAuth2.AppSecret = appSecret;
    var refreUrl = filterContext.RequestContext.HttpContext.Request.Url.ToString();
    var uri = new Uri(refreUrl);
    refreUrl = refreUrl.Replace(":" + uri.Port, "");
    var guid = Guid.NewGuid().ToString();
    var authUrl = OAuth2.GetAuthorizeUrl(authdomain, refreUrl, 1, guid);

    filterContext.Result = new RedirectTopResult(authUrl);
    }
    }
    catch (Exception ex)
    {
    LogHelper.WriteLog(ex, "微信授权限出错,重新授权...");
    }
    }
    }
    }

    2.在控制上添加属性

    using FastDev.WeiXinHelper.Auth;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;

    namespace TestWeiXinWap.Controllers
    {

     [UserAuthorize]

    public class HomeController : Controller
    {
    // GET: Home
    public ActionResult Index()
    {
    var wxUserInfo = OAuth2.WxUserInfo;

    return View(wxUserInfo);
    }

    public ActionResult Remove()
    {
    OAuth2.WxUserInfo = null;

    return View("Index");
    }
    }
    }

    以上代码替换自己的AppID 和AppSecret 和授权域名,从哪个页面访问,授权完成会回调到 当前页面。

    点击下载  所属dll

  • 相关阅读:
    我的IT之路2011(二)
    用三层架构画类图的基本结构
    计算机教育学术交流会
    我的IT之路2011(一)
    Canvas中元素的定位
    对话框中的数据绑定(WPF)
    {Binding}详释 (WPF)
    Polyline的问题
    How do I sort groups of data items?(WPF)
    Excel Services OverView系列--3使用Excel Web Services操作Excel工作薄
  • 原文地址:https://www.cnblogs.com/flish/p/9732940.html
Copyright © 2011-2022 走看看