2011-11-14 19 views
5

मैं जा रहा हूँ स्थापित करने के लिए के माध्यम सेकैसे ASP.NET सुरक्षा प्रतिरूपण

http://www.codeassociate.com/caapi/html/T_CA_Common_Security_Impersonate.htm

मैं कड़ी मेहनत से कोड डोमेन \ उपयोगकर्ता नाम और पासवर्ड के लिए नहीं करना चाहती।

क्या मौजूदा विंडोज उपयोगकर्ता प्रमाण-पत्र प्राप्त करना और इसे पास करना संभव है?

उत्तर

3

नहीं, आप ग्राहक पर उपयोगकर्ता का पता नहीं लगा सकते हैं और अपने खाते का प्रतिरूपण नहीं कर सकते हैं।

एक संभावित समाधान, ...

मैं अगर यह काम करेगा पता नहीं है, और मैं यह नहीं कह रहा हूँ कि यह एक अच्छा विचार है, लेकिन आप अपने क्रेडेंशियल्स के लिए उपयोगकर्ता का संकेत दे सकता है, तो आप प्रोग्रामेटिक प्रतिरूपण का उपयोग करने में सक्षम हो सकता है।

/// <summary> 
/// Leverages the Windows API (advapi32.dll) to programmatically impersonate a user. 
/// </summary> 
public class ImpersonationContext : IDisposable 
{ 
    #region constants 

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

    #endregion 

    #region global variables 

    private WindowsImpersonationContext impersonationContext; 
    private bool impersonating; 

    #endregion 

    #region unmanaged code 

    [DllImport("advapi32.dll")] 
    private static extern int LogonUserA(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); 

    #endregion 

    #region constructors 

    public ImpersonationContext() 
    { 
     impersonating = false; 
    } 

    /// <summary> 
    /// Overloaded constructor and begins impersonating. 
    /// </summary> 
    public ImpersonationContext(string userName, string password, string domain) 
    { 
     this.BeginImpersonationContext(userName, password, domain); 
    } 

    #endregion 

    #region impersonation methods 

    /// <summary> 
    /// Begins the impersonation context for the specified user. 
    /// </summary> 
    /// <remarks>Don't call this method if you used the overloaded constructor.</remarks> 
    public void BeginImpersonationContext(string userName, string password, string domain) 
    { 
     //initialize token and duplicate variables 
     IntPtr token = IntPtr.Zero; 
     IntPtr tokenDuplicate = IntPtr.Zero; 

     if (RevertToSelf()) 
     { 
      if (LogonUserA(userName, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref token) != 0) 
      { 
       if (DuplicateToken(token, 2, ref tokenDuplicate) != 0) 
       { 
        using (WindowsIdentity tempWindowsIdentity = new WindowsIdentity(tokenDuplicate)) 
        { 
         //begin the impersonation context and mark impersonating true 
         impersonationContext = tempWindowsIdentity.Impersonate(); 
         impersonating = true; 
        } 
       } 
      } 
     } 

     //close the handle to the account token 
     if (token != IntPtr.Zero) 
      CloseHandle(token); 

     //close the handle to the duplicated account token 
     if (tokenDuplicate != IntPtr.Zero) 
      CloseHandle(tokenDuplicate); 
    } 

    /// <summary> 
    /// Ends the current impersonation context. 
    /// </summary> 
    public void EndImpersonationContext() 
    { 
     //if the context exists undo it and dispose of the object 
     if (impersonationContext != null) 
     { 
      //end the impersonation context and dispose of the object 
      impersonationContext.Undo(); 
      impersonationContext.Dispose(); 
     } 

     //mark the impersonation flag false 
     impersonating = false; 
    } 

    #endregion 

    #region properties 

    /// <summary> 
    /// Gets a value indicating whether the impersonation is currently active. 
    /// </summary> 
    public bool Impersonating 
    { 
     get 
     { 
      return impersonating; 
     } 
    } 

    #endregion 

    #region IDisposable implementation 

    ~ImpersonationContext() 
    { 
     Dispose(false); 
    } 

    public void Dispose() 
    { 
     Dispose(true);    
    } 

    protected virtual void Dispose(bool disposing) 
    { 
     if (disposing) 
     { 
      if (impersonationContext != null) 
      { 
       impersonationContext.Undo(); 
       impersonationContext.Dispose(); 
      } 
     } 
    } 

    #endregion  
} 

का तरीका यहां बताया वर्ग को लागू है::

using (ImpersonationContext context = new ImpersonationContext("user", "password", "domain")) 
{ 
    if (context.Impersonating) 
    { 
     //impersonating 
    } 
} 
+0

तक पहुँचने हो जाएगा उपयोगकर्ता पहले से ही समाधान काम नहीं करता है in..this लॉग इन किया है, तो पर अनुमति है सुनिश्चित करें। यदि आप किसी अन्य खाते (यानी सेवा खाता) का प्रतिरूपण करना चाहते हैं तो यह काम करता है ... जो मैं पसंद करता हूं। –

+0

[देखें] (http://www.codeproject.com/KB/cs/zetaimpersonator.aspx) –

+1

@UweKeim: यह मेरे ऊपर जैसा है जैसा ही है। –

6

आप साइट पर विंडोज एकीकृत प्रमाणीकरण का उपयोग कर रहे मान लिया जाये, तो आप उपयोगकर्ता की साख उपयोगकर्ता का उपयोग कर प्राप्त कर सकते हैं यहां कक्षा है कि आप उपयोग कर सकते हैं। पहचान।

जोड़ें संदर्भ:

using System.Security.Principal; 

इस का प्रयोग करें नेटवर्क पर वर्तमान प्रयोक्ता का रूप धारण करने के लिए।

WindowsIdentity wi = (WindowsIdentity)User.Identity; 
WindowsImpersonationContext wic = null; 

try 
{ 
    wic = wi.Impersonate(); 

    if (wi.IsAuthenticated) 
    { 
     //Do stuff here on network as Current User 
     // i.e. asyncFileUpload.SaveAs(location); 
    } 

} 
catch(Exception ex) 
{ 
    //Log Error Here 

    if (wic != null) 
     wic.Undo(); 

    return; 
} 
finally 
{ 
    if (wic != null) 
     wic.Undo(); 
} 

यकीन है कि लॉग इन हुए प्रयोक्ता नेटवर्क संसाधन वे

+1

बस एक नोट: आपको अपने पकड़ में 'wic.Undo() 'कॉल करने की आवश्यकता नहीं है। आखिरकार ब्लॉक सफलतापूर्वक पूरा होने के बाद या कैच ब्लॉक समाप्त होने के बाद भी चलाएगा। – Joshua

संबंधित मुद्दे