Last active
July 10, 2021 01:18
-
-
Save sdwh/1edba17e4d5d6bc64bd4d73dfb8b1a83 to your computer and use it in GitHub Desktop.
[MVC5 Attributes] #ASP.NET
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 允許其他網站 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); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 可有可無的權限驗證 | |
// 允許使用者是 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