Skip to content

Instantly share code, notes, and snippets.

@sdwh
Last active July 10, 2021 01:18
Show Gist options
  • Save sdwh/1edba17e4d5d6bc64bd4d73dfb8b1a83 to your computer and use it in GitHub Desktop.
Save sdwh/1edba17e4d5d6bc64bd4d73dfb8b1a83 to your computer and use it in GitHub Desktop.
[MVC5 Attributes] #ASP.NET
// 允許其他網站 CROS Get Data from Action
using System.Web.Mvc;
namespace ProjectSpace.Filters
{
public class AllowCORSAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
filterContext.RequestContext.HttpContext.Response.AddHeader("Access-Control-Allow-Origin", "*");
base.OnActionExecuting(filterContext);
}
}
}
// 可有可無的權限驗證
// 允許使用者是 annoymous ,但如果有經過 authenticate 更好,但沒有也沒關係的情境
using System.Web.Mvc;
using System.Web.Mvc.Filters;
namespace ProjectSpace.Filters
{
public class DispensableAuthAttribute : ActionFilterAttribute, IAuthenticationFilter
{
public void OnAuthentication(AuthenticationContext filterContext)
{
}
public void OnAuthenticationChallenge(AuthenticationChallengeContext filterContext)
{
var user = filterContext.HttpContext.User;
if (user == null || !user.Identity.IsAuthenticated)
{
Console.WriteLine("Dispensable..");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment