检查用户是否已激活的自定义属性
我正在创建一个自定义属性来检查用户的帐户是否已激活。我需要以某种方式获取当前用户的用户名。我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果用户未经身份验证,我使用以下代码来重定向用户。这是通过重写“OnActionExecuting”方法来更改控制器和操作(如果它们未经身份验证)来完成的。此示例显示重定向到默认的 mvc 登录页面 /Account/LogOn。
编辑:显然,然后将此属性放在控制器类的顶部,但您知道......:)
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.
Edit: Obviously then put this attribute at the top of your controller classes, but you knew that... :)