2017-02-21 18 views
0

我有一個IAuthenticationFilter將檢查用戶組中的SharePoint:檢索Windows標識爲IAuthenticationFilter

public class BasicAuthFilter : ActionFilterAttribute, IAuthenticationFilter 
    { 
     public void OnAuthentication(AuthenticationContext filterContext) 
     { 
      string userLoginName = filterContext.RequestContext.HttpContext.User.Identity.Name; 
      if (SecurityManager.Auth(userLoginName)) 
       return; 
      else 
       filterContext.Result = new RedirectResult(new UrlHelper(filterContext.RequestContext).Action("AccessDenied", "Error")); 
     } 

     ... 
    } 
} 

它會在每次請求運行,但除了ErrorController

[AllowAnonymous] 
public class ErrorController : Controller 
    ... 

    // Display view and link for "Logout" 
    public ActionResult AccessDenied() 
    { 
     return View(); 
    } 

    // GET: Logout 
    [OutputCache(VaryByParam = "*", Duration = 0, NoStore = true)] // disable caching 
    public ActionResult Logout() 
    { 
     string currentUser = User.Identity.Name; 
     int AuthenticationAttempts = 0; 

     if (Session["AuthenticationAttempts"] == null || !int.TryParse(Convert.ToString(Session["AuthenticationAttempts"]), out AuthenticationAttempts)) 
      AuthenticationAttempts = 0; 

     AuthenticationAttempts += 1; 

     if (AuthenticationAttempts == 1) 
     { 
      Session["PrevUser"] = User.Identity.Name; 
      Session["AuthenticationAttempts"] = AuthenticationAttempts; 
      return new HttpUnauthorizedResult(); 
     } 
     else if (string.Compare(Convert.ToString(Session["PrevUser"]), currentUser, true) == 0) // Somehow it will have echo back, ignore it 
     { 
      return new HttpUnauthorizedResult(); 
     } 
     else 
     { 
      Session.Abandon(); 
      Session.Clear(); 
      return RedirectToAction("Index", "Home"); 
     } 
    } 
} 

Error Controller回報HttpUnauthorizedResult ,瀏覽器會提示登錄。我可以從User.Identity.Name中獲取ErrorController的新用戶名。

然而,當它重定向到HomeController,用戶重置爲原來的,我試過以下,但仍然是相同的

filterContext.RequestContext.HttpContext.User.Identity.Name 
filterContext.HttpContext.User.Identity.Name 
filterContext.Principal.Identity.Name 

難道我錯過了什麼,或者我應該分配用戶輸入後本金?

回答

0

對於任何人遇到同樣的問題,請確保你已經用IIS進行測試。

此方法工作但無法在IISExpress中工作。