The [RESTAuthorization]
is being ignored and instead jump into the code to Get all the Country without checking for the Rest Authorization filter.
Here is the code for RESTAuthorization
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MyWebsite.Repository;
namespace MyWebsite.API.Attributes
{
public class RESTAuthorizeAttribute : AuthorizeAttribute
{
private ISecurityRepository _repository;
public RESTAuthorizeAttribute()
: this(new SecurityRepository())
{
}
public RESTAuthorizeAttribute(ISecurityRepository repository)
{
_repository = repository;
}
private const string _securityToken = "token";
public override void OnAuthorization(AuthorizationContext filterContext)
{
if (Authorize(filterContext))
{
return;
}
HandleUnauthorizedRequest(filterContext);
}
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
base.HandleUnauthorizedRequest(filterContext);
}
private bool Authorize(AuthorizationContext actionContext)
{
try
{
HttpRequestBase request = actionContext.RequestContext.HttpContext.Request;
string token = request.Params[_securityToken];
string ip = _repository.GetIP(request);
return _repository.IsTokenValid(token, ip, request.UserAgent);
}
catch (Exception)
{
return false;
}
}
}
}
Here's the code for get all country. The RestAuthorize
is being ignore
[RESTAuthorize]
[HttpGet]
public IEnumerable<dtoCountry> GetAllCountry()
{
try
{
return _repository.GetAllCountry().ToList();
}
catch (UnauthorizedAccessException)
{
throw new HttpResponseException(HttpStatusCode.Unauthorized);
}
catch (Exception)
{
throw new HttpResponseException(HttpStatusCode.InternalServerError);
}
}