Login Login
MORE

WIDGETS

Widgets

Wanted articles
Who is online?
Article tools

CSharp:Sicurezza Impersonificazione

From Aino Wiki

Jump to: navigation, search

Esempio

Ci sono contesti, es quello WEB per cui alcune operazioni devono essere fatte da determinati utenti.
Esempio preso da learn.microsoft.com
Si usano due finzioni, quella che impersonnifica il thread come un determinato utente e quella che ripristina la situazione precedente

using System;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Principal;
 
namespace VF_Common
{
    public class Security
    {
        #region Impersonificazione di Windows
        public const int LOGON32_LOGON_INTERACTIVE = 2;
        public const int LOGON32_PROVIDER_DEFAULT = 0;
 
        WindowsImpersonationContext m_impersonationContext;
 
        [DllImport("advapi32.dll")]
        public 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)]
        public static extern int DuplicateToken(IntPtr hToken,
        int impersonationLevel,
        ref IntPtr hNewToken);
 
        [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        public static extern bool RevertToSelf();
 
        [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
        public static extern bool CloseHandle(IntPtr handle);
 
 
        public bool ImpersonateValidUser(String userName, String domain, String password)
        {
            WindowsIdentity tempWindowsIdentity;
            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)
                    {
                        tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
                        m_impersonationContext = tempWindowsIdentity.Impersonate();
                        if (m_impersonationContext != null)
                        {
                            CloseHandle(token);
                            CloseHandle(tokenDuplicate);
                            return true;
                        }
                    }
                }
            }
            if (token != IntPtr.Zero)
                CloseHandle(token);
            if (tokenDuplicate != IntPtr.Zero)
                CloseHandle(tokenDuplicate);
            return false;
        }
        public void UndoImpersonation()
        {
            try
            {
                if (m_impersonationContext != null)
                    m_impersonationContext.Undo();
            }
            catch {}
        }
        #endregion
 
        public static SecureString ToSecureStr(string text)
        {
            SecureString strOut = new SecureString();
            foreach (char c in text)
            {
                strOut.AppendChar(c);
            }
            return strOut;
        }
    }
}

Un esempio d'uso:

public HttpResponseMessage CreateBareRepository(string relPathBareRepoName)
{
     HttpResponseMessage response = null;
     string errorMsg = string.Empty;
     string execOutput = string.Empty;
 
    m_Security.ImpersonateValidUser(m_userName, m_domain, m_password);
 
    response = Request.CreateResponse(HttpStatusCode.OK, execOutput);    // <--- OK !
    response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
 
    m_Security.UndoImpersonation();
    return response;
}

Mappa e Link


C# | Sicurezza


Visual Studio | MS SQL | Dizionario


Parole chiave:

Author