75 lines
2.6 KiB
C#
75 lines
2.6 KiB
C#
using Microsoft.AspNetCore.Authentication;
|
||
using Microsoft.AspNetCore.Authentication.Cookies;
|
||
using Microsoft.AspNetCore.Authorization;
|
||
using Microsoft.AspNetCore.Http;
|
||
using Microsoft.AspNetCore.Mvc;
|
||
using Microsoft.AspNetCore.Mvc.Filters;
|
||
using Microsoft.IdentityModel.Logging;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Security.Claims;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace Mini.Web.WebHelper
|
||
{
|
||
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
|
||
public class AuthorizeRedirect : ActionFilterAttribute
|
||
{
|
||
private const string IS_AUTHORIZED = "isAuthorized";
|
||
|
||
public string RedirectUrl = "~/Base/Account/UnAuthorized";
|
||
|
||
public string Roles;
|
||
public AuthorizeRedirect()
|
||
{
|
||
|
||
}
|
||
public AuthorizeRedirect(string ActionName)
|
||
{
|
||
this.Roles = ActionName;
|
||
}
|
||
//protected override bool AuthenticationSchemes(System.Web.HttpContextBase httpContext)
|
||
//{
|
||
// LogHelper.Info("resutl:" + (new string[] { "B105" }).Any(httpContext.User.IsInRole));
|
||
// bool isAuthorized = base.AuthorizeCore(httpContext);
|
||
|
||
// httpContext.Items.Add(IS_AUTHORIZED, isAuthorized);
|
||
|
||
// return isAuthorized;
|
||
//}
|
||
public override void OnActionExecuting(ActionExecutingContext filterContext)
|
||
{
|
||
|
||
string rightcodes = filterContext.HttpContext.User.FindFirstValue(ClaimTypes.Role);
|
||
if (rightcodes!=null && rightcodes.Contains(string.Format("[{0}]", Roles)))
|
||
{
|
||
return;
|
||
}
|
||
var account = filterContext.HttpContext.Request.Query["weixincrmaccount"];
|
||
if (!string.IsNullOrEmpty(account))
|
||
{
|
||
return;
|
||
}
|
||
//如果没有登陆,则直接从url地址中找账号id
|
||
|
||
HttpRequest httpRequest = filterContext.HttpContext.Request;
|
||
RedirectResult redirectResult = new RedirectResult(RedirectUrl);
|
||
filterContext.Result = redirectResult;
|
||
}
|
||
|
||
//public virtual void OnAuthorization(AuthorizationFilterContext filterContext)
|
||
//{
|
||
//base.OnAuthorization(filterContext);
|
||
//string ss = base.Roles;
|
||
//var isAuthorized = filterContext.HttpContext.Items[IS_AUTHORIZED] != null ? Convert.ToBoolean(filterContext.HttpContext.Items[IS_AUTHORIZED]) : false;
|
||
|
||
//if (!isAuthorized && filterContext.RequestContext.HttpContext.User.Identity.IsAuthenticated)
|
||
//{
|
||
// filterContext.RequestContext.HttpContext.Response.Redirect(RedirectUrl);
|
||
//}
|
||
|
||
//}
|
||
}
|
||
}
|