Created
March 23, 2017 08:03
-
-
Save tayyebi/eac5464c6f1bb70aa5a45c4c131a6829 to your computer and use it in GitHub Desktop.
Global authentication attribute for MVC
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
using System; | |
using System.Net; | |
using System.Net.Http; | |
using System.Web; | |
using System.Web.Http.Controllers; | |
using System.Web.Http.Filters; | |
namespace Gordafarid.Controllers | |
{ | |
internal class GordibuteAttribute : ActionFilterAttribute, IActionFilter | |
{ | |
public override void OnActionExecuting(HttpActionContext actionContext) | |
{ | |
bool CanAccess = false; | |
try | |
{ | |
// if (filterContext.HttpContext.Session["Username"] != null) | |
// string Username = actionContext.ActionArguments["Username"].ToString(); | |
var queryStringCollection = HttpUtility.ParseQueryString(actionContext.Request.RequestUri.Query); | |
string Username = queryStringCollection["Username"]; | |
CanAccess = true; | |
} | |
catch | |
{ | |
CanAccess = false; | |
} | |
if (!CanAccess) | |
actionContext.Response = new HttpResponseMessage(HttpStatusCode.Unauthorized); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment