0

Is this possible?

After I authenticate into my app, I want to check against the database if the logged user is imported or not. If not, it should be imported.

I want to do this right after the windows authentication has been successfully made.

Is there another way to do this?

2
  • 1
    What does "imported" mean? Commented Feb 23, 2014 at 15:10
  • Imported in my database. Commented Feb 23, 2014 at 17:08

1 Answer 1

1

Windows credentials will be checked whenever a user attempts to execute an action that is decorated with the [Authorize] filter. You could simply derive a new filter from that one:

public class ImportAuthorizeAttribute : AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        if (base.AuthorizeCore(httpContext))
        {
             //If the base authorize returns true, then authorization has successfully
             //occurred. 
             var identity = httpContext.User.Identity;
             //You'll need to figure this part out
             ImportIdentityIfNotPresent(identity);
        }
    }
}

Now, you can restrict access by applying it at the action level:

[ImportAuthorizeAttribute]
public ActionResult Create()

Or at the controller level:

[ImportAuthorizeAttribute]
public class AdminController : Controller

Or even globally by editing FilterConfig.cs in `/App_Start':

public class FilterConfig
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
        filters.Add(new Code.Filters.MVC.ImportAuthorizeAttribute());
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Is it ok to make this check at every query? My application should not allow any user to view a page as long as it isn't logged in with user credentials.
Yes, it is alright. Remember, HTTP is stateless, so if you are securing a page, authorization will happen with each request.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.