Monday, January 23, 2012

Centrate div with css

#warning {
width:740px;
left:50%;
top:100px;
margin-left:-370px;
position:fixed;
z-index:10000;
}

//fix for ie6
* html #warning {position:absolute;}

Thursday, January 5, 2012

Run application c# with another username

using System;
using System.Web;
using System.Security.Principal;
using System.Runtime.InteropServices;
using System.IO;
using System.Configuration;

///
/// FileAccess
public class FileAccessUtility
{

#region Constructor
public FileAccessUtility()
{
}
#endregion
#region Const
public const int LOGON32_LOGON_INTERACTIVE = 2;
public const int LOGON32_PROVIDER_DEFAULT = 0;
WindowsImpersonationContext impersonationContext;
#endregion
#region DllImport
[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);

#endregion


#region Methods




public void Copy(string Source, string Destionation)
{
string user, pass, domain;
user = ""; //ConfigurationManager.AppSettings["UserFileAccess"].ToString();
pass = ""; //ConfigurationManager.AppSettings["PassUserFileAccess"].ToString();
domain = ""; //ConfigurationManager.AppSettings["DomainUserFileAccess"].ToString();

if (impersonateValidUser(user, domain, pass))
{
File.Copy(Source, Destionation);
undoImpersonation();
}
else
{
throw new Exception("Autentificarea a esuat pentru userul: " + domain + "\\" + user);
}
}

public void WriteToFile(string filePath, ref byte[] Buffer)
{
string user = "", pass = "", domain = "";
//user = ConfigurationManager.AppSettings["UserFileAccess"].ToString();
//pass = ConfigurationManager.AppSettings["PassUserFileAccess"].ToString();
//domain = ConfigurationManager.AppSettings["DomainUserFileAccess"].ToString();

if (impersonateValidUser(user, domain, pass))
{
WriteFile(filePath, ref Buffer);
undoImpersonation();
}
else
{
throw new Exception("Autentificarea a esuat pentru userul: " + domain + "\\" + user);
}
}

public byte[] ReadFromFile(string filePath)
{
string user = "", pass = "", domain = "";
byte[] buffer;

//user = ConfigurationManager.AppSettings["UserFileAccess"].ToString();
//pass = ConfigurationManager.AppSettings["PassUserFileAccess"].ToString();
//domain = ConfigurationManager.AppSettings["DomainUserFileAccess"].ToString();

if (impersonateValidUser(user, domain, pass))
{
buffer = ReadFile(filePath);
undoImpersonation();
return buffer;
}
else
{
throw new Exception("Autentificarea a esuat pentru userul: " + domain + "\\" + user);
}
}

private void WriteFile(string filePath, ref byte[] Buffer)
{
if (!Directory.Exists(Path.GetDirectoryName(filePath)))
Directory.CreateDirectory(Path.GetDirectoryName(filePath));
FileStream newFile = new FileStream(filePath, FileMode.CreateNew);
newFile.Write(Buffer, 0, Buffer.Length);
newFile.Close();
}

private byte[] ReadFile(string filePath)
{
byte[] buffer;
FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);

int length = (int)fileStream.Length; // get file length
buffer = new byte[length]; // create buffer
int count; // actual number of bytes read
int sum = 0; // total number of bytes read

// read until Read method returns 0 (end of the stream has been reached)
while ((count = fileStream.Read(buffer, sum, length - sum)) > 0)
sum += count; // sum is a buffer offset for next reading


fileStream.Close();
return buffer;


}

private 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);
impersonationContext = tempWindowsIdentity.Impersonate();
if (impersonationContext != null)
{
CloseHandle(token);
CloseHandle(tokenDuplicate);
return true;
}
}
}
}
if (token != IntPtr.Zero)
CloseHandle(token);
if (tokenDuplicate != IntPtr.Zero)
CloseHandle(tokenDuplicate);
return false;
}

private void undoImpersonation()
{
impersonationContext.Undo();
}




#endregion
}





Call the class

FileAccessUtility fileAcces;
byte[] myData;
HttpPostedFile myFile;
if (fileUploadDocumentSolicitant.PostedFile.InputStream.Length < 7000000)
{
Random rnd = new Random();
fileAcces = new FileAccessUtility();
}

Wednesday, January 4, 2012

C# - Serialize/Deserialize object

public static string SerObj(object rspJ)
{
string serObj = null;

MemoryStream ms = new MemoryStream();
BinaryFormatter bf1 = new BinaryFormatter();
bf1.Serialize(ms, rspJ);
serObj = Convert.ToBase64String(ms.ToArray());

return serObj;
}

public static string SerObjXML(object rspJ, Type type)
{
string serObj = null;

MemoryStream ms = new MemoryStream();
XmlSerializer bf1 = new XmlSerializer(type);
bf1.Serialize(ms, rspJ);
ms.Position = 0;
StreamReader sr = new StreamReader(ms);
serObj = sr.ReadToEnd();

return serObj;
}

public static object DeSerObjXML(string xml, Type type)
{
XmlSerializer xs = new XmlSerializer(type);
MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(xml));
XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
return xs.Deserialize(memoryStream);
}

public static
RspTestXML DeSerObj(string stringObj)
{
RspTestXML rspTestXML = null;

byte[] theByteArray = Convert.FromBase64String(stringObj);

MemoryStream ms1 = new MemoryStream(theByteArray);
BinaryFormatter bf11 = new BinaryFormatter();
ms1.Position = 0;

rspTestXML = (
RspTestXML)bf11.Deserialize(ms1);

return rspTestXML;
}


private static Byte[] StringToUTF8ByteArray(string pXmlString)
{
UTF8Encoding encoding = new UTF8Encoding();
byte[] byteArray = encoding.GetBytes(pXmlString);
return byteArray;
}

Create Trigger log

create TRIGGER [dbo].[table_Logger] ON [dbo].table
AFTER INSERT,DELETE,UPDATE
AS
BEGIN
SET NOCOUNT ON;

declare @Actiune varchar(50),
@HostName varchar(50),
@SQL nvarchar(500),
@d int,
@i int,
@event int

set @HostName = null

select @HostName = hostname
from master..sysprocesses
where loginame = user_name()

select @HostName = isnull(@HostName, '?'), @d = 0, @i = 0
if exists (select top 1 1 from inserted)
select @i = 1
if exists (select top 1 1 from deleted)
select @d = 1

if @d=1 and @i=1
select @event = 1 /*update*/
else
begin
if @i=1 and @d = 0
select @event = 2 /*insert*/
if @d = 1 and @i = 0
select @event = 3 /*delete*/
end


if @event = 1
begin
insert into table_LOG
select *, 'deleted' , system_user, 'update', getdate(), @HostName
from deleted

insert into table_LOG
select *, 'inserted' , system_user, 'update', getdate(), @HostName
from inserted
end
else if @event = 2
insert into OJ_GrupuriDetaliiSuplimentare_LOG
select *, 'inserted' , system_user, 'insert', getdate(), @HostName
from inserted
else if @event = 3
insert into table_LOG
select *, 'deleted' , system_user, 'delete', getdate(), @HostName
from deleted

end


Replace Table With your table
The log table has the following structure:
create table "table_LOG"
(
.............
[TabelSursa] [varchar](100) NULL,
[NumeUtilizatorLog] [varchar](100) NULL,
[Actiune] [varchar](20) NULL,
[DataLog] [datetime] NULL,
[HostName] [varchar](50) NULL
)