Pregunta

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?

¿Fue útil?

Solución

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());
    }
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top