我在MVC 3站點中使用Forms Auth和ASP Universal Membership Provider。爲了方便用戶,我們堅持使用Cookie。如何在使用ASP表單持久性Cookie /票據時禁用用戶?
FormsAuthentication.SetAuthCookie(model.UserName, true)
當我們禁用這樣的會員供應商的用戶:
memUser.IsApproved = model.IsActive;
provider.UpdateUser(memUser);
,如果他們有他們的cookie仍然可以在該網站。這與本文中描述的內容類似:http://stackoverflow.com/questions/5825273/how-do-you-cancel-someones-persistent-cookie-if-their-membership-is-no-longer-v
我們在我們的控制器上使用授權屬性,我知道這在技術上比驗證更具授權。但肯定是overloap,所以我試圖找出什麼是最好的MVC方法來檢查用戶是否實際上沒有被禁用?根據成員資格數據庫檢查用戶的自定義AuthorizeAttribute?我缺少Forms驗證以使驗證無效的明顯設置/方法?
更新:
這裏基本上是我跟去 - 我們使用自定義權限被拒絕頁,我們我們使用,以更好地告知用戶,他們沒有權利對他們沒有登錄我添加了IsApproved檢查。當您將該屬性放在Controller或Action上並且它返回false時調用AuthorizeCore會調用HandleUnauthorizedRequest。
public class CustomAuthorization : AuthorizeAttribute
{
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
if (!filterContext.HttpContext.User.Identity.IsAuthenticated || !Membership.GetUser(filterContext.HttpContext.User.Identity.Name).IsApproved)
{
filterContext.Result = new HttpUnauthorizedResult();
// in the case that the user was authenticated, meaning he has a ticket,
// but is no longer approved we need to sign him out
FormsAuthentication.SignOut();
}
else
{
var permDeniedRouteVals = new System.Web.Routing.RouteValueDictionary() { { "controller", "MyErrorController" }, { "action", "MyPermissionDeniedAction" }, { "area", null } };
filterContext.Result = new RedirectToRouteResult(permDeniedRouteVals);
}
}
protected override bool AuthorizeCore(System.Web.HttpContextBase httpContext)
{
// since persisting ticket,
// adding this check for the case that the user is not active in the database any more
return base.AuthorizeCore(httpContext) && Membership.GetUser(httpContext.User.Identity.Name).IsApproved;
}
}
用法:
[CustomAuthorization()]
public class MyController
這是一個自定義的角色提供者嗎?我想了解更多信息,因爲這看起來像是一個常見問題。我在應用程序中使用了一些不同的角色,但對角色提供者不熟悉。我只是使用Roles.AddUserToRole方法。有更多的角色提供者嗎? –
@CDSmith您可以使用默認角色提供程序和aspnet數據庫中的相關內容,只需要所有受保護活動的基本「登錄」角色。只有最後一段需要任何自定義提供者。 –
所以基本上,只要我的所有用戶都屬於某個角色,並且我的拒登功能將他們從角色中移除,他們就無法再訪問受保護的操作了,對嗎?這是我目前擁有的 –