It seems that the custom authorization attribute will work. Here is an example implementation:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)] public class RequiresSerialValidationAttribute : AuthorizeAttribute { public override void OnAuthorization(AuthorizationContext filterContext) { bool hasValidSerial = false; if (filterContext.HttpContext.Request.IsAuthenticated) { string userName = filterContext.HttpContext.User.Identity.Name; if (!string.IsNullOrWhiteSpace(userName)) { string serial = string.Empty;
You must decorate action methods with this attribute:
[RequireSerialValidation] public ActionResult SomeAction() { }
The attribute will cause a redirect to your call, where you ask the user for your serial number. Assuming everything is going well, you store their series somewhere (the session can work here or create an encrypted cookie), and then redirect back to the original action. In the second attempt, you have already confirmed that the action is allowed, so the redirect does not occur.
Your authentication service may be what you want. In this example, I assume that you are using dependency injection and that you have configured a global dependency converter. Given that your IYourAuthService might look like this (excluding other methods):
public IYourAuthService { bool IsSerialValidForUser(string userName, string serial); }
with implementation like this:
public YourAuthService : IYourAuthService { public bool IsSerialValidForUser(string userName, string serial) { using(var context = new YourEntityFrameworkDbContext()) { return context.Users.Any(u => u.UserName.Equals(userName, StringComparison.OrdinalIgnoreCase) && u.Serial.Equals(serial, StringComparison.OrdinalIgnoreCase)); } } }
It is assumed that your database has a table named User (or Users ), and UserName and Serial fields in this table. StringComparison.OrdinalIgnoreCase allows you to do case-insensitive, culture-insensitive matching of the strings you are trying to compare.