Here is my example on static reflection(i have used INotifyPropertyChanged, but a simple code as i don't pass around the interface)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.ComponentModel;
namespace StaticReflection
{
public class Customer : INotifyPropertyChanged
{
private string employer = String.Empty;
private string mobile = String.Empty;
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
public string Employer
{
get { return this.employer; }
set
{
if (value != this.employer)
{
this.employer = value;
NotifyPropertyChanged(this.PropertyName(c => c.Employer));
}
}
}
public string Mobile
{
get { return this.mobile; }
set
{
if (value != this.mobile)
{
this.mobile = value;
NotifyPropertyChanged(this.PropertyName(c => c.Mobile));
}
}
}
}
public static class CustomerExtensions
{
public static string PropertyName(this Customer target, Expression<Func<Customer, object>> expression)
{
return new PropertySpecifier<Customer>(expression).PropertyName;
}
}
public class PropertySpecifier<T>
{
//http://joelabrahamsson.com/entry/getting-property-and-method-names-using-static-reflection-in-c-sharp - Use this article to build a accomplished! PropertySpecifier function
public PropertySpecifier(Expression<Func<T, object>> expression)
{
if (expression == null) return;
if (expression.Body is MemberExpression)
{
this.PropertyName = (expression.Body as MemberExpression).Member.Name;
}
}
public string PropertyName
{
get;
set;
}
}
}
Friday, 21 October 2011
Wednesday, 28 September 2011
Create PDB always
For my reference
http://blogs.msdn.com/b/yash/archive/2007/10/12/pdb-files-what-are-they-and-how-to-generate-them.aspx
Project > Properties > Build > Advanced > Output > Debug Info
Always create PDB
http://blogs.msdn.com/b/yash/archive/2007/10/12/pdb-files-what-are-they-and-how-to-generate-them.aspx
Project > Properties > Build > Advanced > Output > Debug Info
Always create PDB
Tuesday, 13 September 2011
Events vs Delegates
This rather confused me for a long time, but found a great article
http://blog.monstuff.com/archives/000040.html
Briefly
Events & Interfaces, Events Invocation, Event Signature and Event accessors
1. Events are ALWAYS multi-cast.
2. Events can be done away with delegates, but it is similar to exposing a Public member field than a Property.
3. Helps encapsulation - So interfaces don't support fields, but events - We can't declare delegate fields in Interface.
4. Only defined class can raise the event, but fields(delegates) can be raised outside of the class.
5. Standard signature of (sender, eventargs) - we can use EventHandler.
6. Event accessors - have add and remove - may be used for logging?
So my two cents with an example
public class MoneyWithdrawnEvent : EventArgs
{
public decimal Amount { get; set; }
public MoneyWithdrawnEvent(decimal amount)
{
this.Amount = amount;
}
}
public class BankService
{
public delegate void Notify(EventArgs e);
public event Notify amountWithdrawn;
public void Withdraw(decimal amount)
{
//Business
amountWithdrawn(new MoneyWithdrawnEvent(amount));
}
}
public class ATMService
{
IServiceFactory factory;
public ATMService(IServiceFactory factory)
{
}
public void Withdraw(decimal amount)
{
BankService service = factory.Create();
BankService.Notify notifyUser = new BankService.Notify(this.AlertUser);
notifyUser(new MoneyWithdrawnEvent(amount)); //This is wrong, as we don't the state(outcome) of the transaction, and
// allows this kind of business errors where alert before
// withdrawing money.
//service.amountWithdrawn(); 1. You can't invoke event, so it will be the responsiblity
// of bank to raise the money withdrawn.
service.Withdraw(amount);
}
private void AlertUser(EventArgs alert)
{
}
}
public interface IServiceFactory
{
BankService Create();
}
http://blog.monstuff.com/archives/000040.html
Briefly
Events & Interfaces, Events Invocation, Event Signature and Event accessors
1. Events are ALWAYS multi-cast.
2. Events can be done away with delegates, but it is similar to exposing a Public member field than a Property.
3. Helps encapsulation - So interfaces don't support fields, but events - We can't declare delegate fields in Interface.
interface Temp
{
event EventHandler notify;
EventHandler eventHandler;
}
{
event EventHandler notify;
EventHandler eventHandler;
}
4. Only defined class can raise the event, but fields(delegates) can be raised outside of the class.
5. Standard signature of (sender, eventargs) - we can use EventHandler
6. Event accessors - have add and remove - may be used for logging?
So my two cents with an example
public class MoneyWithdrawnEvent : EventArgs
{
public decimal Amount { get; set; }
public MoneyWithdrawnEvent(decimal amount)
{
this.Amount = amount;
}
}
public class BankService
{
public delegate void Notify(EventArgs e);
public event Notify amountWithdrawn;
public void Withdraw(decimal amount)
{
//Business
amountWithdrawn(new MoneyWithdrawnEvent(amount));
}
}
public class ATMService
{
IServiceFactory factory;
public ATMService(IServiceFactory factory)
{
}
public void Withdraw(decimal amount)
{
BankService service = factory.Create();
BankService.Notify notifyUser = new BankService.Notify(this.AlertUser);
notifyUser(new MoneyWithdrawnEvent(amount)); //This is wrong, as we don't the state(outcome) of the transaction, and
// allows this kind of business errors where alert before
// withdrawing money.
//service.amountWithdrawn(); 1. You can't invoke event, so it will be the responsiblity
// of bank to raise the money withdrawn.
service.Withdraw(amount);
}
private void AlertUser(EventArgs alert)
{
}
}
public interface IServiceFactory
{
BankService Create();
}
Thursday, 14 July 2011
Create PFX with the Asymmetric keys genereated during CSR phase
This will not work with CNG(KSP)
Steps 1
1. When we create key/pair, store the KeyContainerName.
2. Sign CSR with created keys and get the certificate(one of CER/DER/PEM/CRT) format.
3. Open the certificate using x509certificate2 class, create CspParameter class. Assign the KeyContainerName to CspParamater class.
4. Create RsaCryptoProvider class using CspParamater, and assign it to PrivateKey of x509certificate2.
5. Using Export(pfx, password) option.
6. Clear RsaCryptoProvider using PersistCsp = false, and Clear().
Note
1. If you don't pass valid KeyContainerName, default will be created. But assigning to PrivateKey property will throw exception about Key mismatch(after all they are strongly related).
2. Clear the KeyContainer for better security purposes.
Sample code to get u started.
CspParameters p = new CspParameters();
p.ProviderName = "Microsoft Enhanced Cryptographic Provider v1.0";
p.ProviderType = 1;
p.KeyContainerName = "your container name";
p.KeyNumber = (int)AT_KEYEXCHANGE;
RSACryptoServiceProvider csp = new RSACryptoServiceProvider(p);
X509Certificate2 cer = new X509Certificate2("sss.cer");
cer.PrivateKey = csp;
byte[] bytes = cer.Export(X509ContentType.Pfx, "12345");
FileStream stream = new FileStream("CASigned.pfx", FileMode.CreateNew);
stream.Write(bytes, 0, bytes.Length);
stream.Close();
X509Certificate2 c1 = new X509Certificate2("CASigned.pfx", "12345");
bool value = c1.HasPrivateKey;
Steps 1
1. When we create key/pair, store the KeyContainerName.
2. Sign CSR with created keys and get the certificate(one of CER/DER/PEM/CRT) format.
3. Open the certificate using x509certificate2 class, create CspParameter class. Assign the KeyContainerName to CspParamater class.
4. Create RsaCryptoProvider class using CspParamater, and assign it to PrivateKey of x509certificate2.
5. Using Export(pfx, password) option.
6. Clear RsaCryptoProvider using PersistCsp = false, and Clear().
Note
1. If you don't pass valid KeyContainerName, default will be created. But assigning to PrivateKey property will throw exception about Key mismatch(after all they are strongly related).
2. Clear the KeyContainer for better security purposes.
Sample code to get u started.
CspParameters p = new CspParameters();
p.ProviderName = "Microsoft Enhanced Cryptographic Provider v1.0";
p.ProviderType = 1;
p.KeyContainerName = "your container name";
p.KeyNumber = (int)AT_KEYEXCHANGE;
RSACryptoServiceProvider csp = new RSACryptoServiceProvider(p);
X509Certificate2 cer = new X509Certificate2("sss.cer");
cer.PrivateKey = csp;
byte[] bytes = cer.Export(X509ContentType.Pfx, "12345");
FileStream stream = new FileStream("CASigned.pfx", FileMode.CreateNew);
stream.Write(bytes, 0, bytes.Length);
stream.Close();
X509Certificate2 c1 = new X509Certificate2("CASigned.pfx", "12345");
bool value = c1.HasPrivateKey;
Labels:
Asymmetric Key container,
c#,
Certificates
Wednesday, 13 July 2011
Certificate file types
Two types of encoding used in certificate
1. DER(Distinguished Encoding Rule) - DER encoded certificate.
2. PEM - Base64 encoded certificate - starts with BEGIN CERTIFICATE and ends with END CERTIFICATE.
Above can also used and extensions.
Common extensions
-----------------
1. CRT - Most common among *nix systems
2. CER - alternate form of .CRT (Microsoft Convention)
The only time CRT and CER can safely be interchanged is when the encoding type can be identical. (ie PEM encoded CRT = PEM encoded CER)
1. DER(Distinguished Encoding Rule) - DER encoded certificate.
2. PEM - Base64 encoded certificate - starts with BEGIN CERTIFICATE and ends with END CERTIFICATE.
Above can also used and extensions.
Common extensions
-----------------
1. CRT - Most common among *nix systems
2. CER - alternate form of .CRT (Microsoft Convention)
The only time CRT and CER can safely be interchanged is when the encoding type can be identical. (ie PEM encoded CRT = PEM encoded CER)
Monday, 4 July 2011
Respecting windows UAC while using LsaOpenPolicy
Please use POLICY_VIEW_LOCAL_INFORMATION = 1;
so your code will become
LsaOpenPolicy(SystemName, ref LSA_OBJECT_ATTRIBUTES,
(int)POLICY_VIEW_LOCAL_INFORMATION, out IntPtr)
so your code will become
LsaOpenPolicy(SystemName, ref LSA_OBJECT_ATTRIBUTES,
(int)POLICY_VIEW_LOCAL_INFORMATION, out IntPtr)
Subscribe to:
Posts (Atom)