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,允許您臨時向用戶添加特定角色,並在一段時間後過期該角色。這不會與存儲在數據庫中的用戶分配的角色相關聯,除非可能說只有具有特定角色的用戶才能被提升。

這種方式,你可以簡單地進行密碼驗證,臨時添加的角色,用戶角色列表,然後將標準授權屬性將讓他們進來。

相關問題