微信授权封装,欢迎使用

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

 

微信授权封装,欢迎使用

上一篇:微信公众号开发上传图文素材带有卡片小程序报错:errcode=45166,errmsg = invalid content hint


下一篇:钉钉新功能,微信最该学