需要某些动作重新验证 [英] Require re-authentication for certain actions
本文介绍了需要某些动作重新验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有关如更改电子邮件设置或管理员活动的某些行动,我想完成的动作之前用户重新进行身份验证。是否有在ASP.NET MVC 3这样的良好格局?
For certain actions like changing email settings or administrator activities, I want users to re-authenticate before the action is completed. Is there a good pattern for doing this in ASP.NET MVC 3?
推荐答案
您可以创建用户名
,密码
,你想更改的字段(<$ C $您ActionMethod C>电子邮件)。比验证在 [HttpPost]
您的数据这些数据。如果授权有成功的,改变它,如果没有错误添加到的ModelState
。
Descpription
You can create your ActionMethod with Username
, Password
and the field you want to change (Email
) for example. Than validate this data in the [HttpPost]
of your data. If the authorization has success, change it and if not add the error to the ModelState
.
使用一个ViewModel了点。
Use a ViewModel for that.
public class ChangeEmailViewModel
{
public string Username { get; set; }
public string Password { get; set; }
public string EmailAddress { get; set; }
}
public ActionResult ChangeEmail()
{
return this.View(new ChangeEmailViewModel());
}
public Action ChangeEmail(ChangeEmailViewModel model)
{
// authorize
bool isAuthorized = // your logic.
if (isAuthorized)
{
// change email
} else
{
ModelState.AddModelError("Username", "Username is not valid");
}
return this.View(model);
}
这篇关于需要某些动作重新验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文