6

在MVC中执行操作之前,如何强制用户重新进行验证?在MVC应用程序中重新验证用户的操作

我们正在使用Windows身份验证。我们希望确保用户正在执行一些操作(并且在用户忘记锁定工作站时阻止其他用户执行这些操作)。

理想我只希望能够写一个扩展Authorize属性:

namespace AuthTest.Controllers 
{ 
    [Authorize(Roles="MyApp")] 
    public class HomeController : Controller 
    {  
     public ActionResult Index() 
     { 
      // A regular action 
      return View(); 
     } 

     [ReAuthenticate] 
     public ActionResult CriticalAction() 
     { 
      // Do something important 
      return View(); 
     } 
    } 
} 

看来,我可以强制用户重新输入其凭据由具有自定义属性ReAuthenticate发出HTTP 401响应在AuthorizeCore方法中。然而,这需要一些挂羊头卖狗肉,因为Html.ActionLink将派遣两个请求:

protected override bool AuthorizeCore(HttpContextBase httpContext) 
{ 
    bool ok = base.AuthorizeCore(httpContext); 
    if (!ok) return false; 

    if (httpContext.Session["ReAuthCnt"] == null) 
    { 
     httpContext.Session["ReAuthCnt"] = 1; 
     return false; 
    } 
    else if ((int) httpContext.Session["ReAuthCnt"] < 2) 
    { 
     httpContext.Session["ReAuthCnt"] = (int)httpContext.Session["ReAuthCnt"] + 1; 
     return false; 
    } 
    else 
    { 
     httpContext.Session["ReAuthCnt"] = 0; 
     return true; 
    } 
} 

有没有更好的方式来完成的重新授权?

+0

你是什么意思的重新认证? –

+0

你为什么想要?如果用户通过了身份验证,那么他们就可以通过身份验证。如果您无法维护身份验证,则可以考虑使用OAuth。也许如果你将问题描述得多一点,我们可以建议做什么。 – griegs

+0

我已经更新了有关场景和我所尝试的更多细节的问题。 – user2813199

回答

0

如果用户正在执行POST,是否可以向该表单添加用户名和密码字段,然后在控制器中使用Active Directory或使用ActionFilter验证凭据?

-2

你可以从这里得到知道我是在这里做

[Authorize(Roles = "admin,superadmin")]//Controler Authorization 
     public class ControlController : Controller 
     { 

      [Authorize(Roles = "superadmin")]//action Authorization 
      public ActionResult youraction() 
      { 
       return View(); 
      } 
     } 
0

角色基础授权如果你是企业获准实际使用的一种形式重新验证它们(换句话说,有一个网页,他们键入用户名和密码),那么你可以做到以下几点。

ReauthorizeAttribute

// custom page where user does type user/pass 
private string revalidateLoginUrl = "/account/reauth"; 
private bool? hasReauthenticated = false; 

protected override bool AuthorizeCore(HttpContextBase httpContext) 
{ 
    var authUrl = HttpContext.Request.Url; 
    hasReauthenticated = httpContext.Session[authUrl] as bool? 

    // could be just (hasReauthenticated) 
    // this look a bit more readable 
    return (hasReauthenticated == true); 
} 

public override void OnAuthorization(AuthorizationContext filterContext) 
{ 
    if (filterContext == null) 
    { 
    throw new ArgumentNullException("filterContext"); 
    } 

    var isAuthorized = AuthorizeCore(filterContext.HttpContext); 

    var authUrl = filterContext.HttpContext.Request.Url; 
    filterContext.HttpContext.Session[authUrl] = false; 

    if (!isAuthorized) 
    { 
    HandleUnauthorizedRequest(filterContext); 
    } 
} 

protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext) 
{ 
    // something like this 
    var fullUrl = validateLoginurl + "?returnUrl=" 
    + HttpUtility.UrlEncode(revalidaetLoginUrl); 

    filterContext.HttpContext.Response.Redirect(validateLoginUrl); 
} 

ReauthModel

public class ReauthModel 
{ 
    public string Username { get; set; } 
    public string Password { get; set; } 
    public string ReturnUrl { get; set; } 
} 

AccoountController.cs (Validate a username and password against Active Directory?

using System.DirectoryServices.AccountManagement; 

public ActionResult Reauth(string returnUrl) 
{ 
    var model = new ReauthModel(); 
    model.ReturnUrl = returnUrl; 

    return View(model); 
} 

[ValidateAntiForgeryToken] 
public ActionResult Reauth(ReauthModel model) 
{ 
    using(PrincipalContext pc = new 
    PrincipalContext(ContextType.Domain, "YOURDOMAIN")) 
    { 
    // validate the credentials 
    bool isValid = pc.ValidateCredentials("myuser", "mypassword"); 
    if (isValid) 
    { 
     Session[model.ReturnUrl] = true; 
     return RedirectTolocal(model.ReturnUrl); 
    } 
    } 

    // not authenticated 
    return RedirectToAction("?"); 

    //or 
    model.Username = string.Empty; 
    model.Passsword = string.Empty; 

    return View(model); 
} 

我想你可以根据ReauthModel的看法是什么样子可想而知。

注:这应该除了你正在使用,不能代替使用任何其他authorizeattribute的。由于用户在网站上输入了用户名和密码,因此需要才能使用SSL(即使它是内部的)。

0

我会以不同的方式处理这个问题。这听起来像你真正想要的是一个临时的证书提升,类似于UAC或Sudo,但在你的网站上。如果一段时间过去了,用户可能需要再次输入凭据才能访问这些功能。

我会采取的方法是创建一个自定义IPrincipal,允许您临时向用户添加特定角色,并在一段时间后过期该角色。这不会与存储在数据库中的用户分配的角色相关联,除非可能说只有具有特定角色的用户才能被提升。

这种方式,你可以简单地进行密码验证,临时添加的角色,用户角色列表,然后将标准授权属性将让他们进来。

相关问题