检查用户是否已激活的自定义属性

发布于 2024-11-29 15:10:26 字数 673 浏览 1 评论 0原文

我正在创建一个自定义属性来检查用户的帐户是否已激活。我需要以某种方式获取当前用户的用户名。我正在使用 FormsAuthentication.SetAuthCookie() 登录用户。

在任何控制器方法上,如果未经授权,我想重定向到特定路由。可以这样吗?我就是这样开始的。

public class ActivatedAuthroizeAttribute : System.Web.Mvc.AuthorizeAttribute
{
    protected override void HandleUnauthorizedRequest(System.Web.Mvc.AuthorizationContext filterContext)
    {
        // Check to see if user is authorized. 
        DefaultUnitOfWork unitOfWork = new DefaultUnitOfWork();
        //User user = UnitOfWork.UserRepository.IsUserActivated(FormsAuthentication.GetAuthCookie(.Value["Username"]);

        base.HandleUnauthorizedRequest(filterContext);
    }

I'm looking to create a custom attribute that checks to see if the user's account has been activated. I need to somehow get the username of the current user. I am using FormsAuthentication.SetAuthCookie() to login the user.

On any controller method I want to redirect to a particular route if they're not authorized. Can this be done this way? This is how I started.

public class ActivatedAuthroizeAttribute : System.Web.Mvc.AuthorizeAttribute
{
    protected override void HandleUnauthorizedRequest(System.Web.Mvc.AuthorizationContext filterContext)
    {
        // Check to see if user is authorized. 
        DefaultUnitOfWork unitOfWork = new DefaultUnitOfWork();
        //User user = UnitOfWork.UserRepository.IsUserActivated(FormsAuthentication.GetAuthCookie(.Value["Username"]);

        base.HandleUnauthorizedRequest(filterContext);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我偏爱纯白色 2024-12-06 15:10:26

如果用户未经身份验证,我使用以下代码来重定向用户。这是通过重写“OnActionExecuting”方法来更改控制器和操作(如果它们未经身份验证)来完成的。此示例显示重定向到默认的 mvc 登录页面 /Account/LogOn。

public class RequiresAuthenticationAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    //You can put your check here. This particular
    //check is for default asp.net membership authentication

    if(!filterContext.HttpContext.User.Identity.IsAuthenticated)
    {
        RedirectToLogin(filterContext);
    }
}

private void RedirectToLogin(ActionExecutingContext filterContext)
{
    var redirectTarget = new RouteValueDictionary
    {
        {"action", "LogOn"},
        {"controller", "Account"}
    };

    filterContext.Result = new RedirectToRouteResult(redirectTarget);
    }
}

编辑:显然,然后将此属性放在控制器类的顶部,但您知道......:)

[RequiresAuthentication]
public class HomeController : Controller
{
    // code...
}

I've used the following code to redirect users if they are not authenticated. This is done by overriding the "OnActionExecuting" method to change the controller and action if they are not authenticated. This example shows the redirection to the default mvc login page /Account/LogOn.

public class RequiresAuthenticationAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
    //You can put your check here. This particular
    //check is for default asp.net membership authentication

    if(!filterContext.HttpContext.User.Identity.IsAuthenticated)
    {
        RedirectToLogin(filterContext);
    }
}

private void RedirectToLogin(ActionExecutingContext filterContext)
{
    var redirectTarget = new RouteValueDictionary
    {
        {"action", "LogOn"},
        {"controller", "Account"}
    };

    filterContext.Result = new RedirectToRouteResult(redirectTarget);
    }
}

Edit: Obviously then put this attribute at the top of your controller classes, but you knew that... :)

[RequiresAuthentication]
public class HomeController : Controller
{
    // code...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文