CodeKeep C# Feed Novembre 5th, 2008
Description: Retrieve embedded resource from executing assembly
Link: http://www.codekeep.net/snippets/5b7195ca-da71-4338-b370-0047e21043a4.aspx
Assembly executingAssembly = Assembly.GetExecutingAssembly ();
Stream _resourceStream = null;
string resourceName = executingAssembly
.GetManifestResourceNames ()
.FirstOrDefault(rn=>rn.Contains(Enum.GetName (typeof (ReportType), reportType)));
if (resourceName != null)
_resourceStream = executingAssembly.GetManifestResourceStream (resourceName);
return _resourceStream;

WebReference News Novembre 5th, 2008
->
Many Web sites want to control who has access to what. There are many situations where access control is appropriate, and they can easily become very complex. Here we look at the most highly regarded model–role-based access control (RBAC) system - and find ways to implement it. By Martin Brampton 1103
CodeKeep C# Feed Novembre 5th, 2008
Description: SetSecurity Installer class. this is to grant full trust to the installed assembly
Link: http://www.codekeep.net/snippets/2923ee77-bd65-4881-ad95-22c39caa869a.aspx
//-----------------------------------------------------------------------
//
// Copyright (C) Microsoft Corporation. All rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using System.Diagnostics;
using System.IO;
namespace CustomActions
{
[RunInstaller(true)]
[System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
public sealed partial class SetSecurity : Installer
{
public SetSecurity()
{
InitializeComponent();
}
public override void Install(System.Collections.IDictionary stateSaver)
{
// Call the base implementation.
base.Install(stateSaver);
string allUsersString = this.Context.Parameters["allUsers"];
string solutionCodeGroupName = this.Context.Parameters["solutionCodeGroupName"];
string solutionCodeGroupDescription = this.Context.Parameters["solutionCodeGroupDescription"];
string targetDir = this.Context.Parameters["targetDir"];
string assemblyName = this.Context.Parameters["assemblyName"];
string assemblyCodeGroupName = this.Context.Parameters["assemblyCodeGroupName"];
string assemblyCodeGroupDescription = this.Context.Parameters["assemblyCodeGroupDescription"];
// Note that a code group with solutionCodeGroupName name is created in the
// Install method and removed in the Rollback and Uninstall methods.
// The solutionCodeGroupName must be a unique name to ensure that the
// correct code group is removed during Rollback and Uninstall.
if (String.IsNullOrEmpty(solutionCodeGroupName))
throw new InstallException("Cannot set the security policy. The specified solution code group name is not valid.");
if (String.IsNullOrEmpty(solutionCodeGroupDescription))
throw new InstallException("Cannot set the security policy. The specified solution code group description is not valid.");
if (String.IsNullOrEmpty(targetDir))
throw new InstallException("Cannot set the security policy. The specified target directory is not valid.");
if (String.IsNullOrEmpty(assemblyName))
throw new InstallException("Cannot set the security policy. The specified assembly name is not valid.");
if (String.IsNullOrEmpty(assemblyCodeGroupName))
throw new InstallException("Cannot set the security policy. The specified assembly code group name is not valid.");
if (String.IsNullOrEmpty(assemblyCodeGroupDescription))
throw new InstallException("Cannot set the security policy. The specified assembly code group description is not valid.");
if (stateSaver == null)
throw new ArgumentNullException("stateSaver");
try
{
bool allUsers = String.Equals(allUsersString, "1");
string assemblyPath = Path.Combine(targetDir, assemblyName);
// Note that Install method may be invoked during Repair mode and the code group
// may already exist.
// To prevent adding of another code group, remove the code group if it exists.
try
{
// The solutionCodeGroupName must be a unique name; otherwise, the method might delete wrong code group.
CaspolSecurityPolicyCreator.RemoveSecurityPolicy(allUsers, solutionCodeGroupName);
}
catch {}
CaspolSecurityPolicyCreator.AddSecurityPolicy(
allUsers,
solutionCodeGroupName,
solutionCodeGroupDescription,
assemblyPath,
assemblyCodeGroupName,
assemblyCodeGroupDescription);
stateSaver.Add("allUsers", allUsers);
}
catch (Exception ex)
{
throw new InstallException("Cannot set the security policy.", ex);
}
}
public override void Rollback(System.Collections.IDictionary savedState)
{
// Call the base implementation.
base.Rollback(savedState);
// Check whether the "allUsers" property is saved.
// If it is not set, the Install method did not set the security policy.
if ((savedState == null) || (savedState["allUsers"] == null))
return;
// The solutionCodeGroupName must be a unique name; otherwise, the method might delete wrong code group.
string solutionCodeGroupName = this.Context.Parameters["solutionCodeGroupName"];
if (String.IsNullOrEmpty(solutionCodeGroupName))
throw new InstallException("Cannot remove the security policy. The specified solution code group name is not valid.");
try
{
bool allUsers = (bool) savedState["allUsers"];
CaspolSecurityPolicyCreator.RemoveSecurityPolicy(allUsers, solutionCodeGroupName);
}
catch (Exception ex)
{
throw new InstallException("Cannot remove the security policy.", ex);
}
}
public override void Uninstall(System.Collections.IDictionary savedState)
{
// Call the base implementation.
base.Uninstall(savedState);
// Check whether the "allUsers" property is saved.
// If it is not set, the Install method did not set the security policy.
if ((savedState == null) || (savedState["allUsers"] == null))
return;
// The solutionCodeGroupName must be a unique name; otherwise, the method might delete wrong code group.
string solutionCodeGroupName = this.Context.Parameters["solutionCodeGroupName"];
if (String.IsNullOrEmpty(solutionCodeGroupName))
throw new InstallException("Cannot remove the security policy. The specified solution code group name is not valid.");
try
{
bool allUsers = (bool)savedState["allUsers"];
CaspolSecurityPolicyCreator.RemoveSecurityPolicy(allUsers, solutionCodeGroupName);
}
catch (Exception ex)
{
// Note that throwing an exception might stop the uninstall process.
// To inform the user and stop the uninstall process, throw an exception.
// To continue the uninstall, do not throw the exception.
throw new InstallException("Cannot remove the security policy.", ex);
}
}
}
}

CodeKeep C# Feed Novembre 5th, 2008
Description: code for encryption and decryption
Link: http://www.codekeep.net/snippets/af1cd375-059a-4175-93d7-25eea2c5c660.aspx
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.Configuration;
namespace Cybernetics.CRM40.Office.Word_2007.Add_in
{
public class CryptorEngine
{
private static string m_Key = "Cybernetics CRM 4.0 Word Addin";
/// <summary>
/// Encrypt a string using dual encryption method. Return a encrypted cipher Text
/// </summary>
/// <param name="toEncrypt">string to be encrypted</param>
/// <param name="useHashing">use hashing? send to for extra secirity</param>
/// <returns></returns>
public static string Encrypt(string toEncrypt, bool useHashing)
{
byte[] keyArray;
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
// Get the key from config file
//System.Windows.Forms.MessageBox.Show(key);
if (useHashing)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(m_Key));
hashmd5.Clear();
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(m_Key);
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tdes.CreateEncryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tdes.Clear();
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
/// <summary>
/// DeCrypt a string using dual encryption method. Return a DeCrypted clear string
/// </summary>
/// <param name="cipherString">encrypted string</param>
/// <param name="useHashing">Did you use hashing to encrypt this data? pass true is yes</param>
/// <returns></returns>
public static string Decrypt(string cipherString, bool useHashing)
{
byte[] keyArray;
byte[] toEncryptArray = Convert.FromBase64String(cipherString);
if (useHashing)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(m_Key));
hashmd5.Clear();
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(m_Key);
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tdes.CreateDecryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tdes.Clear();
return UTF8Encoding.UTF8.GetString(resultArray);
}
}
}
