2008-08-15 17 views
11

मैं Exchange 2007 पर मेलबॉक्स/आदि बनाने के लिए वेब इंटरफ़ेस (ASP.NET/C#) के माध्यम से पावरहेल आदेश चलाने की कोशिश कर रहा हूं। जब मैं विजुअल स्टूडियो (कैसिनी) का उपयोग करके पृष्ठ चलाता हूं, तो पृष्ठ सही ढंग से लोड हो जाता है। हालांकि, जब मैं इसे आईआईएस (v5.1) पर चलाता हूं, तो मुझे त्रुटि "अज्ञात उपयोगकर्ता नाम या खराब पासवर्ड" मिलता है। मैंने देखा कि सबसे बड़ी समस्या यह थी कि पावरहेल को मेरे सक्रिय निर्देशिका खाते की बजाय एएसपीएनईटी के रूप में लॉग इन किया गया था। मैं अपने पावरहेल सत्र को किसी अन्य सक्रिय निर्देशिका खाते से प्रमाणित करने के लिए कैसे मजबूर करूं?पावरहेल में सक्रिय निर्देशिका उपयोगकर्ता का प्रतिरूपण कैसे करते हैं?

असल में, स्क्रिप्ट मेरे पास है कि अब तक इस तरह दिखता है:

RunspaceConfiguration rc = RunspaceConfiguration.Create(); 
PSSnapInException snapEx = null; 
rc.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", out snapEx); 

Runspace runspace = RunspaceFactory.CreateRunspace(rc); 
runspace.Open(); 

Pipeline pipeline = runspace.CreatePipeline(); 
using (pipeline) 
{ 
    pipeline.Commands.AddScript("Get-Mailbox -identity 'user.name'"); 
    pipeline.Commands.Add("Out-String"); 

    Collection<PSObject> results = pipeline.Invoke(); 

    if (pipeline.Error != null && pipeline.Error.Count > 0) 
    { 
     foreach (object item in pipeline.Error.ReadToEnd()) 
      resultString += "Error: " + item.ToString() + "\n"; 
    } 

    runspace.Close(); 

    foreach (PSObject obj in results) 
     resultString += obj.ToString(); 
} 

return resultString; 

उत्तर

1

एक्सचेंज 2007 आपको सुरक्षा कारणों से किसी उपयोगकर्ता का प्रतिरूपण करने की अनुमति नहीं देता है। इसका मतलब यह है कि किसी उपयोगकर्ता का प्रतिरूपण करके मेलबॉक्स बनाने के लिए यह असंभव है (फिलहाल)। इस समस्या को हल करने के लिए, मैंने एक वेब सेवा बनाई जो एडी उपयोगकर्ता के तहत चलता है जिसमें ईमेल की गणना आदि की अनुमति होती है। फिर आप पावरहेल तक पहुंच प्राप्त करने के लिए इस webservice तक पहुंच सकते हैं। कृपया आवश्यक सुरक्षा को जोड़ना याद रखें क्योंकि यह संभावित रूप से एक विशाल सुरक्षा छेद हो सकता है।

2

अपने ASP.NET अनुप्रयोग में, आप सही अनुमति के साथ एक वैध ई खाते का रूप धारण करने की आवश्यकता होगी:

http://support.microsoft.com/kb/306158

7

यहां एक कक्षा है जिसका उपयोग मैं उपयोगकर्ता का प्रतिरूपण करने के लिए करता हूं।

using System; 
using System.Data; 
using System.Configuration; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Web.UI.HtmlControls; 

namespace orr.Tools 
{ 

    #region Using directives. 
    using System.Security.Principal; 
    using System.Runtime.InteropServices; 
    using System.ComponentModel; 
    #endregion 

    /// <summary> 
    /// Impersonation of a user. Allows to execute code under another 
    /// user context. 
    /// Please note that the account that instantiates the Impersonator class 
    /// needs to have the 'Act as part of operating system' privilege set. 
    /// </summary> 
    /// <remarks> 
    /// This class is based on the information in the Microsoft knowledge base 
    /// article http://support.microsoft.com/default.aspx?scid=kb;en-us;Q306158 
    /// 
    /// Encapsulate an instance into a using-directive like e.g.: 
    /// 
    ///  ... 
    ///  using (new Impersonator("myUsername", "myDomainname", "myPassword")) 
    ///  { 
    ///   ... 
    ///   [code that executes under the new context] 
    ///   ... 
    ///  } 
    ///  ... 
    /// 
    /// Please contact the author Uwe Keim (mailto:[email protected]) 
    /// for questions regarding this class. 
    /// </remarks> 
    public class Impersonator : 
     IDisposable 
    { 
     #region Public methods. 
     /// <summary> 
     /// Constructor. Starts the impersonation with the given credentials. 
     /// Please note that the account that instantiates the Impersonator class 
     /// needs to have the 'Act as part of operating system' privilege set. 
     /// </summary> 
     /// <param name="userName">The name of the user to act as.</param> 
     /// <param name="domainName">The domain name of the user to act as.</param> 
     /// <param name="password">The password of the user to act as.</param> 
     public Impersonator(
      string userName, 
      string domainName, 
      string password) 
     { 
      ImpersonateValidUser(userName, domainName, password); 
     } 

     // ------------------------------------------------------------------ 
     #endregion 

     #region IDisposable member. 

     public void Dispose() 
     { 
      UndoImpersonation(); 
     } 

     // ------------------------------------------------------------------ 
     #endregion 

     #region P/Invoke. 

     [DllImport("advapi32.dll", SetLastError = true)] 
     private static extern int LogonUser(
      string lpszUserName, 
      string lpszDomain, 
      string lpszPassword, 
      int dwLogonType, 
      int dwLogonProvider, 
      ref IntPtr phToken); 

     [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
     private static extern int DuplicateToken(
      IntPtr hToken, 
      int impersonationLevel, 
      ref IntPtr hNewToken); 

     [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
     private static extern bool RevertToSelf(); 

     [DllImport("kernel32.dll", CharSet = CharSet.Auto)] 
     private static extern bool CloseHandle(
      IntPtr handle); 

     private const int LOGON32_LOGON_INTERACTIVE = 2; 
     private const int LOGON32_PROVIDER_DEFAULT = 0; 

     // ------------------------------------------------------------------ 
     #endregion 

     #region Private member. 
     // ------------------------------------------------------------------ 

     /// <summary> 
     /// Does the actual impersonation. 
     /// </summary> 
     /// <param name="userName">The name of the user to act as.</param> 
     /// <param name="domainName">The domain name of the user to act as.</param> 
     /// <param name="password">The password of the user to act as.</param> 
     private void ImpersonateValidUser(
      string userName, 
      string domain, 
      string password) 
     { 
      WindowsIdentity tempWindowsIdentity = null; 
      IntPtr token = IntPtr.Zero; 
      IntPtr tokenDuplicate = IntPtr.Zero; 

      try 
      { 
       if (RevertToSelf()) 
       { 
        if (LogonUser(
         userName, 
         domain, 
         password, 
         LOGON32_LOGON_INTERACTIVE, 
         LOGON32_PROVIDER_DEFAULT, 
         ref token) != 0) 
        { 
         if (DuplicateToken(token, 2, ref tokenDuplicate) != 0) 
         { 
          tempWindowsIdentity = new WindowsIdentity(tokenDuplicate); 
          impersonationContext = tempWindowsIdentity.Impersonate(); 
         } 
         else 
         { 
          throw new Win32Exception(Marshal.GetLastWin32Error()); 
         } 
        } 
        else 
        { 
         throw new Win32Exception(Marshal.GetLastWin32Error()); 
        } 
       } 
       else 
       { 
        throw new Win32Exception(Marshal.GetLastWin32Error()); 
       } 
      } 
      finally 
      { 
       if (token != IntPtr.Zero) 
       { 
        CloseHandle(token); 
       } 
       if (tokenDuplicate != IntPtr.Zero) 
       { 
        CloseHandle(tokenDuplicate); 
       } 
      } 
     } 

     /// <summary> 
     /// Reverts the impersonation. 
     /// </summary> 
     private void UndoImpersonation() 
     { 
      if (impersonationContext != null) 
      { 
       impersonationContext.Undo(); 
      } 
     } 

     private WindowsImpersonationContext impersonationContext = null; 

     // ------------------------------------------------------------------ 
     #endregion 
    } 
} 
1

आप एक पैच की जरूरत हो सकती।

से: http://support.microsoft.com/kb/943937

एक आवेदन एक प्रयोक्ता का रूप धारण नहीं कर सकते और फिर 2007 पर्यावरण एक एक्सचेंज सर्वर में आदेशों को चलाने के लिए Windows PowerShell

इस समस्या को हल करने के लिए, स्थापित अद्यतन रोलअप 1 एक्सचेंज सर्वर 2007 सर्विस पैक 1.