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