Skip to content

Instantly share code, notes, and snippets.

@tayyebi
Created March 23, 2017 08:03
Show Gist options
  • Save tayyebi/eac5464c6f1bb70aa5a45c4c131a6829 to your computer and use it in GitHub Desktop.
Save tayyebi/eac5464c6f1bb70aa5a45c4c131a6829 to your computer and use it in GitHub Desktop.
Global authentication attribute for MVC
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