码迷,mamicode.com
首页 > 微信 > 详细

微信授权封装,欢迎使用

时间:2018-09-30 20:02:03      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:rem   .com   class   home   引用   cat   ast   gen   use   

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

 

微信授权封装,欢迎使用

标签:rem   .com   class   home   引用   cat   ast   gen   use   

原文地址:https://www.cnblogs.com/flish/p/9732940.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!