Asp.net mvc 3 MVC3授权:从另一个操作调用已授权的操作是一种糟糕的形式吗?
从另一个操作调用控制器操作时是否需要使用Asp.net mvc 3 MVC3授权:从另一个操作调用已授权的操作是一种糟糕的形式吗?,asp.net-mvc-3,forms-authentication,redirecttoaction,authorize-attribute,Asp.net Mvc 3,Forms Authentication,Redirecttoaction,Authorize Attribute,从另一个操作调用控制器操作时是否需要使用重定向到操作?目前我只是直接调用它们,因为我不想让它们返回,因此我绕过Authorize标签来执行我的一个操作(这就是我想要的) 您能告诉我这是否是坏表单吗?如果是,我应该创建多个新操作来设置客户端cookie,还是直接在LogOn()操作中设置它们 我是否可以改为将SwitchClient设置为私有,然后将公共授权操作设置为仅由客户端管理员使用?然后,将通过登录操作调用私有操作,但除非用户作为管理员进行身份验证,否则无法访问该操作 这是我的密码:
重定向到操作
?目前我只是直接调用它们,因为我不想让它们返回,因此我绕过Authorize标签来执行我的一个操作(这就是我想要的)
您能告诉我这是否是坏表单吗?如果是,我应该创建多个新操作来设置客户端cookie,还是直接在LogOn()
操作中设置它们
我是否可以改为将SwitchClient
设置为私有,然后将公共授权操作设置为仅由客户端管理员使用?然后,将通过登录
操作调用私有操作,但除非用户作为管理员进行身份验证,否则无法访问该操作
这是我的密码:
[HttpGet]
[CustomAuthorizeAccess(Roles = "Administrator", RedirectResultUrl = "Unauthorized")]
public ActionResult SwitchClient(string client)
{
if (Request.Cookies["Client"] == null)
{
HttpCookie clientCookie = new HttpCookie("Client", client);
Response.Cookies.Add(clientCookie);
}
else
{
Response.Cookies["Client"].Value = client;
}
return new RedirectResult(Request.UrlReferrer.AbsolutePath);
}
[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
if (ModelState.IsValid)
{
if (MembershipService.ValidateUser(model.UserName, model.Password))
{
FormsService.SignIn(model.UserName, model.RememberMe);
if (Url.IsLocalUrl(returnUrl))
{
return Redirect(returnUrl);
}
else
{
//Add user's role to cookies (assumes each user only has one role)
string role = Roles.GetRolesForUser(model.UserName).First();
HttpCookie roleCookie = new HttpCookie("Role", role);
if (role == "client1")
{
SwitchClient("client1");
}
else if (role == "client2")
{
SwitchClient("client2");
}
else if (role == "Administrator" || role == "client3")
{
SwitchClient("client3");
}
//Make role cookie persistent for 7 days
//if user selected "Remember Me"
if (model.RememberMe)
{
roleCookie.Expires = DateTime.Today.AddDays(7);
}
if (Response.Cookies["Role"] != null)
{
Response.Cookies["Role"].Value = null;
Response.Cookies.Remove("Role");
}
Response.Cookies.Add(roleCookie);
return RedirectToAction("Index", "Home");
}
}
else
{
ModelState.AddModelError("", "The user name or password provided is incorrect.");
}
}
// If we got this far, something failed, redisplay form
return View(model);
}
我会将“switch client”逻辑重构为私有方法或实用程序类。两个控制器操作方法都将调用私有方法
这样代码和您的意图就不会那么混淆。这是否绕过了授权?如果是这样的话,那么绝对不要这样做。内部方法和外部方法(无论是公共/私有方法还是公开/未公开到服务)之间存在差异。据我所知,它确实绕过了,因为在
LogOn()
操作中,我已经调用了FormsService.SignIn(model.UserName,model.RememberMe)在我调用SwitchClient
(这适用于任何客户机,无论他们是否担任管理员
角色。感谢阅读pst的评论后我所想的!