這可能嗎?MVC asp.net - Windows身份驗證重定向到指定的操作
當我進入我的應用程序進行身份驗證後,我想檢查數據庫是否導入了登錄用戶。如果不是,則應該導入。
我想在windows身份驗證成功後立即執行此操作。
是否有另一種方法可以做到這一點?
這可能嗎?MVC asp.net - Windows身份驗證重定向到指定的操作
當我進入我的應用程序進行身份驗證後,我想檢查數據庫是否導入了登錄用戶。如果不是,則應該導入。
我想在windows身份驗證成功後立即執行此操作。
是否有另一種方法可以做到這一點?
只要用戶嘗試執行由[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
甚至全局編輯FilterConfig.cs
在`/ App_Start'中:
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
filters.Add(new Code.Filters.MVC.ImportAuthorizeAttribute());
}
可以在每個查詢中進行檢查嗎?只要沒有用戶憑據登錄,我的應用程序就不應允許任何用戶查看頁面。 – tzortzik
是的,它沒問題。請記住,HTTP是無狀態的,因此如果您要保護頁面,每個請求都會進行授權。 –
「導入」是什麼意思? –
導入我的數據庫。 – tzortzik