I have two entity sets like below:
public class Serial
{
[HiddenInput(DisplayValue=false)]
public int SerialID { get; set; }
[HiddenInput(DisplayValue=false)]
public string Id { get; set; }
[Required(ErrorMessage="Please provide your membership serial")]
[StringLength(16,ErrorMessage="This field can't be longer as of 16 characters.")]
public string UserSerial { get; set; }
}
AND
public class Subscription
{
[HiddenInput(DisplayValue=false)]
public int SubscriptionID { get; set; }
[Required(ErrorMessage="Please provide a subscription code.")]
public string AdminSerial { get; set; }
}
I would like to create a custom authorization attribute to design my action methods within my controllers with following scenario:
I would like to check if the any value of
UserSerialin Serial Entity not equal to any value ofAdminSerialin Subscription Entity. If the above condition become true so theActionResultmethod itself should be executed else the CustomAuthorizeAttributeshould redirect it to another action method, here is what i tried but it isn't working am i missing something?
public class RequireSerial : AuthorizeAttribute
{
EFDbContext db = new EFDbContext();
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
if (!db.Subscriptions.Any(s => s.AdminSerial.Equals(db.Serials.Any())))
{
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { controller = "Serials", action = "Create" }));
}
else
{
// Execute the Action method itself
}
}
}
I tried to put this RequireSerial custom authorize attribute on the top of action methods but nothing really happens.
[RequireSerial]
public ViewResult Checkout()
{
return View();
}
Any help would be appreciated.