Wednesday, June 27, 2012

Using SysInternals ProcessExplorer to find the cause of “access denied” error

Often when you try to rebuild your project or even just delete or rename a file, you get the “access denied” error. For example, I run my application from the command line and while it runs I try to rebuild it in Visual Studio:

Such “access denied” issues are also caused by system services like for example name-it-source-control caching service. You just can’t delete your files becase a service locks them.

One of the simplest ways to locate the true culprit – the application or the service which locks the access to the specified file/files – is to use the ProcessExplorer from SysInternals.

In PE, just navigate to Find/Find Handle or DLL. ProcessExplorer will find out and show the list of processes which have active handles to your file:

The example demonstrates the situation described at the beginning – the cause of the access denied to ConsoleApplication55.exe was the ConsoleApplication55.exe process running in the operation system. However, you can expect other valuable findings, like for example the TSVNCache.exe.

Monday, June 18, 2012

IIS 7.5 Integrated Security with no prompt for credentials

If you try to configure an IIS site to use integrated security and still get the prompt for credentials, here are few key things to validate.

First, make sure that Anonymous Authentication is turned OFF for the site:

Second, enable integrated security in Interner Explorer (Options/Advanced and checkin the “Enable Integrated Windows Authentication” option).

Third, add your website to Local Intranet zone and select at least “Automatic logon only in Intranet Zone” option under Options/Security Settings/Local intranet/Custom level).

Fourth, make sure the user and application server are in the same domain.

Wednesday, June 13, 2012

Facebook OAuth2 adventures

OAuth2 is a simple and reliable SSO protocol and what’s great is that it’s implemented by at least few well-known services, like Google, Microsoft or Facebook. I’ve decided to follow the specification and implement SSO gateways for these three.

Google – no issues, works like a charm.

Microsoft – the same.

Facebook – since don’t use Facebook, I’ve expected to get into issues. First, to register the application in Facebook you need their account. So I create one, something like “Test Developer” with one of my emails. I confirm the email by clicking their link and I am there, Facebook welcome.

So I go to the developer section and try to register the application. Facebook says that “there is a chance that this is not a legitimate account, you need to provide the phone number so that we can SMS you and verify the account. Note that the phone number will not be available for registration with other Facebook accounts”. Well, I am not going to spent one of my phone numbers on test account. I give up and decide to register a new, legitimate account.

Once again, I create an account, this time with all valid information. I confirm my email address and again I go to the developer section. The same story, “this is probably not a legitimate account […]” so I enter my phone number.

Great, they send me the SMS message, I input the number from text message and submit the page.

“We still don’t believe that this is a legitimate account. To prove it, please scan your personal ID card of your country so that first name, last name and birth date are clearly visible and submit the image here”.

This is enough for me. I am not going that far, I will ask someone who uses Facebook to assist me with the app registration. But I don’t like the privacy policy and I am going to delete the account.

Well, I find the option to delete the account, I answer the question and my account is deleted.

Is it?

I get an email which says that “thank you, your account has been locked, if you ever decide to come back, please login again with your username and password”.

Great.

Friday, June 1, 2012

How to implement XPO’s Soft Delete pattern for NHibernate

Soft Delete is a persistence pattern where instead of removing entities physically you just mark them as deleted by setting a fixed value in one of columns.

For example, in XPO (DevExpress Persistence Objects) an additional int column called GCRecord holds a null value (not deleted) or anything than null (deleted).

There’s a long discussion whether or not such approach is a pattern or an antipattern but this is beyond this post. What I would like to show is how to get soft deletes with NHibernate so that you can migrate your XPO application to NHibernate and keep your database untouched.

Filtering the data

There are two issues to solve. First – NHibernate should automatically append “filters” so that all queries will select only the data which is not deleted. I’ve blogged on this few months ago. An example code which sets filters for XPO-compatible database is:

// add this for fluent configuration
config.AddFilterDefinition( 
   new NHibernate.Engine.FilterDefinition( 
       "GCRecordFilter", null, new Dictionary<string, IType>(), true ) );
 
// create filters programatically
foreach ( var mapping in config.ClassMappings )
{
    mapping.AddFilter( "GCRecordFilter", "GCRecord is null" );
    foreach ( var property in mapping.PropertyIterator )
        if ( property.Value is Bag )
        {
            Bag bagProperty = (Bag)property.Value;
 
            bagProperty.AddFilter( "GCRecordFilter", "GCRecord is null" );
        }
}

and then you just enable filters:

_session.EnableFilter( "GCRecordFilter" );

Because of filters, NHibernate will append the “GCRecrod is null” to queries and subqueries thus simulating the way XPO filters the data.

Note that NHibernate’s feature of filtering is way, way more advanced – you are free to define any filter you need while XPO has one builtin filter which cannot be easily changed (can it be changed at all?).

Persisting the data

The remaining task is to teach NHibernate to mark records as deleted when it comes to deleting data. This is done with an event listener:

public class SoftDeleteEventListener : DefaultDeleteEventListener
{
    protected override void DeleteEntity( IEventSource session, object entity,
        EntityEntry entityEntry, bool isCascadeDeleteEnabled,
        IEntityPersister persister, ISet transientEntities )
    {
        // be smart - if the entity implements a marking interface - softdelete it
        if ( entity is ISoftDeletable )
        {
            var e = (ISoftDeletable)entity;
            e.GCRecord = 1;
 
            CascadeBeforeDelete( session, persister, entity, entityEntry, transientEntities );
            CascadeAfterDelete( session, persister, entity, transientEntities );
        }
        // else delete it
        else
        {
            base.DeleteEntity( session, entity, entityEntry, isCascadeDeleteEnabled,
                              persister, transientEntities );
        }
    }
}

which is applied by:

config.SetListener( NHibernate.Event.ListenerType.Delete, new SoftDeleteEventListener() );

Audit logging the data

Event listeners can be used not only to soft delete the data. Suppose for example that your entity holds a simple auditing info (insert date, modify date):

public interface ISoftDeletable
{
    int? GCRecord { get; set; }
    DateTime? ModifyDate { get; set; }
    DateTime? InsertDate { get; set; }
}

You can set up an event lister to automatically set values to the two columns:

public class CustomSaveEventListener : DefaultSaveEventListener
{
    protected override object PerformSaveOrUpdate( SaveOrUpdateEvent evt )
    {
        ISoftDeletable entity = evt.Entity as ISoftDeletable;
        if ( entity != null )
            ProcessEntityBeforeInsert( entity );
 
        return base.PerformSaveOrUpdate( evt );
    }
 
    internal virtual void ProcessEntityBeforeInsert( ISoftDeletable entity )
    {
        if ( entity.InsertDate == null )
            entity.InsertDate = DateTime.Now;
        entity.ModifyDate = DateTime.Now;
    }
}
 
public class CustomUpdateEventListener : DefaultUpdateEventListener
{
    protected override object PerformSaveOrUpdate( SaveOrUpdateEvent evt )
    {
        ISoftDeletable entity = evt.Entity as ISoftDeletable;
        if ( entity != null )
            ProcessEntityBeforeInsert( entity );
 
        return base.PerformSaveOrUpdate( evt );
    }
 
    internal virtual void ProcessEntityBeforeInsert( ISoftDeletable entity )
    {
        if ( entity.InsertDate == null )
            entity.InsertDate = DateTime.Now;
        entity.ModifyDate = DateTime.Now;
    }
}

and apply listeners:

config.SetListener( NHibernate.Event.ListenerType.Save, new CustomSaveEventListener() );
config.SetListener( NHibernate.Event.ListenerType.Update, new CustomUpdateEventListener() );

Putting all this toghether, it is possible to migrate your XPO persistence layer to NHibernate without losing the Soft Delete feature.