MVC asp.net - windows身份验证重定向到指定的操作

时间:2014-02-23 14:17:07

标签: asp.net asp.net-mvc redirect windows-authentication

这可能吗?

在我对我的应用程序进行身份验证后,如果已导入已记录的用户,我想检查数据库。如果没有,则应该导入。

我想在成功完成Windows身份验证后立即执行此操作。

还有其他办法吗?

1 个答案:

答案 0 :(得分:1)

每当用户尝试执行使用[Authorize]过滤器修饰的操作时,都会检查Windows凭据。您只需从该过滤器中派生出一个新的过滤器:

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);
        }
    }
}

现在,您可以通过在操作级别应用来限制访问:

[ImportAuthorizeAttribute]
public ActionResult Create()

或在控制器级别:

[ImportAuthorizeAttribute]
public class AdminController : Controller

甚至在全球范围内编辑`/ App_Start'中的FilterConfig.cs

public class FilterConfig
{
    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
        filters.Add(new Code.Filters.MVC.ImportAuthorizeAttribute());
    }