1

我知道標題是一口。我已經掌握了大部分的東西。我只需要確認我是否可以做我想做的事情。如何通過WebRequest調用MVC Action並通過Active Directory驗證請求?

我正在使用ASP.NET MVC 3.我有一個應用程序具有控制器,我喜歡使用Web服務。控制器上有一個方法,它返回一個字符串,它是json。該方法根據活動目錄驗證用戶。

執行上述WebRequest的應用程序也是一個MVC應用程序。此應用程序(爲了查詢沒有特定用戶名和密碼的AD)正在使用web.config中的模擬。該應用程序模擬有權查詢AD的帳戶;但是,頁面上的用戶信息(例如他們在哪個組)是我驗證的內容。

總之(我不完全理解這部分),模擬是嚴格的,因此ASP.NET可以查詢Active Directory。當我查詢活動目錄的信息時,加載頁面的用戶仍然被視爲自己。

廣告代碼如下所示(此代碼的工作):

public static ADUserInfo GetUserInfo(IPrincipal User) 
    { 
     StringBuilder userAdInfo = new StringBuilder(); 
     ADUserInfo userInfo = new ADUserInfo(); 
     String domain = ConfigurationManager.AppSettings["ADdomain"]; 

     try 
     { 
      using (var context = new PrincipalContext(ContextType.Domain, domain)) 
      { 
       if (User == null) 
        userAdInfo.AppendLine("User is null."); 
       else if (User.Identity == null) 
        userAdInfo.AppendLine(" User is not null. User.Identitiy is."); 
       else 
        userAdInfo.AppendLine(" Neither User nor User.Identity is null. " + 
         User.Identity.Name); 

       using (var user = UserPrincipal.FindByIdentity(context, User.Identity.Name)) 
       { 
        userInfo.FullName = user.Name; 
        userInfo.Email = user.EmailAddress; 
        userInfo.AssociateId = user.EmployeeId; 
        userInfo.DomainName = User.Identity.Name; 
        userInfo.SamAccountName = user.SamAccountName; 
        userInfo.DistinguishedUserName = user.DistinguishedName; 
       } 
      } 
     } 
     catch (Exception e) 
     { 
      LogUtil.WriteException(e); 
     } 
     return userInfo; 
    } 

IIS站點此應用程序不允許匿名訪問。

使用AD信息的服務方法正常工作。這個問題似乎是通過WebRequest傳遞憑據來調用此方法並返回JSON。

我的WebRequest代碼來調用動作看起來像:

public class WebRequestUtil 
    { 
     public static StreamReader GetWebRequestStream(
      string url, 
      string contentType, 
      bool useDefaultCredentials) 
     { 
      var request = WebRequest.Create(url); 
      request.ContentType = contentType; 
      request.ImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation; 
      //request.UseDefaultCredentials = useDefaultCredentials; 
      //ICredentials ic = new NetworkCredential(); 

      //request.Credentials = 
      var response = (HttpWebResponse)request.GetResponse(); 
      return new StreamReader(response.GetResponseStream()); 
     } 
    } 

我與ImpersonationLevel玩...還沒有工作....

的MVC 3行動被稱爲通過WebRequest的看起來像:

public class ProxyServiceController : Controller 
    { 

     public ProxyServiceController() 
     { 

     } 

     public string CheckForProxy(string applicationName, string associateId) 
     { 
      RequestResultDetails requestDetails = new RequestResultDetails(); 
      string json = string.Empty; 

      //This correctly gets the Active directory information for the user 
      //and fills out a custom ADUserInfo object. 
      **ADUserInfo adUserInfo = ADService.GetUserInfo(this.User);** 

      try 
      { 

       if (!ADService.DoesUrlDataMatchAD(
           adUserInfo, 
           associateId) 
        ) 
       { 
        throw new Exception(StaticText.UserDataMismatch); 
       } 

       resultList = //query db for data given the associateId 

       if (resultList.ListIsNotNullOrEmpty()) 
       { 
        requestDetails.RelationshipExists = true; 
       } 
       else 
       { 
        requestDetails.RelationshipExists = false; 
       } 

       requestDetails.Details = resultList; 

      } 
      catch (Exception e) 
      { 
       LogUtil.WriteException(e); 
       requestDetails.ErrorProcessingRequest = true; 
       requestDetails.ErrorDetails = ErrorProcessing.GetFullExceptionDetails(e); 
      } 

      json = JsonConvert.SerializeObject(requestDetails); 

      LogUtil.Write("json: " + json); 

      return json; 

     } 
}  

所以會發生什麼是,如果我通過像一個URL轉到MVC 3控制器/動作直接在瀏覽器:

http://:90/MyApp/Service.aspx/CheckForProxy/Reporting/555

我可以在頁面上看到正確的JSON。但是,如果我從同一服務器上的另一個應用程序對同一個URL進行WebRequest調用,Active Directory看起來不像它可以被輪詢。這肯定是某種權限問題,但我不確定如何解決它,以便服務看到用戶的Active Directory信息。

這裏的問題是,傳遞給服務的憑證是調用應用程序模擬的帳戶的憑證。我需要更改哪些內容才能讓服務mvc應用程序查看用戶執行WebRequest(以及應用程序是否會打電話,但用戶是否加載應用程序),而不是應用程序模擬的帳戶?

我接受其他想法或方法來處理這種溝通。

溶液由jmrnet COMMENT

哇,那意見是當場上。我不知道什麼樣的網絡魔力正在工作,但我修改我的Web請求的方法:

public static StreamReader GetWebRequestStream(
     string url, 
     string contentType, 
     bool useDefaultCredentials, 
     IPrincipal user) 
    { 

     var impersonationContext = ((WindowsIdentity)user.Identity).Impersonate();    
     var request = WebRequest.Create(url); 

     try 
     { 
      request.ContentType = contentType; 
      request.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequested; 
      request.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials; 
      var response = (HttpWebResponse)request.GetResponse(); 
      return new StreamReader(response.GetResponseStream()); 
     } 
     catch (Exception e) 
     { 
      impersonationContext.Undo(); 
      throw e; 
     } 

    } 

這沿主用戶的身份通過準確。

回答

1

兩個想法:

1)你可以嘗試迫使模擬你讓你的WebRequest調用之前。就像這樣:

var impersonationContext = ((WindowsIdentity)User.Identity).Impersonate(); 
//Make your WebRequest call here... 
impersonationContext.Undo(); 

2)你可以通過在的WebRequest呼叫的用戶,在服務端得到AD用戶身份,然後使用上面的代碼的變化來冒充他們。

下面是更多地談論上面使用的模擬代碼的鏈接: http://msdn.microsoft.com/en-us/library/w070t6ka.aspx

+0

這做到了人!謝謝! – jason

相關問題