且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在ASP.NET MVC 3 Intranet应用程序中对用户进行重新身份验证?

更新时间:2023-02-17 11:00:51

让表单将凭据与请求一起发送以执行该操作,即某些操作要求您提供用户名/密码.使用PrincipalContext ValidateCredentials 方法来确保已输入正确的凭据并检查提供的用户名是否与User.Identity对象中的当前用户名匹配.

Have the form send the credentials along with the request to perform the action, i.e., some actions require that you provide username/password. Use the PrincipalContext ValidateCredentials method to ensure that the proper credentials have been entered and check that the username supplied matches the current username in the User.Identity object.

public ActionResult SensitiveAction( SensitiveModel model, string username, string password )
{
    using (var context = new PrincipalContext(ContextType.Domain))
    {
         if (!string.Equals(this.User.Identity.Name,username,StringComparison.OrdinalIgnoreCase)
             || !context.ValidateCredentials(username,password))
         {
              return View("PermissionDenied");
         }
    }

    ...
}