Failed to get public key for StrongNameKeyPair

I ported the development to another computer, and if I run the project, I have this exception:

Failed to get public key for StrongNameKeyPair.

HibernateException: Failed to create proxy instance

On the original computer, it works fine without problems.

I found on google that this is a problem with some encryption, and I have to try "sn -m n", I don’t know how to do this. sn.exe is located in more folders, I tried some startup from the command line, but it writes:

Could not open registry key - Unable to format error message 00000005

I don’t know if the problem is that NHibernate or not, there are more similar whitch dialogs, and this exception throws out only in one case.

There is an exception part for code exclusion:

public IList<DTO> GetAll(GridSortOptions sortOptions, DTOListModel<DTO> listModel)
{
    return GetAllCriteria(sortOptions, CreateCriteria(), listModel).List<DTO>();
}

None of the projects using the solution uses. I do not understand what exactly these errors mean and what I should look for.

+5
source share
4 answers

NHibernate dynamically creates .NET assemblies (dynaic proxies) and requires their signature. By default, Windows configures the crypto key store at the machine level and saves the keys in C: \ Documents and Settings \ All Users \ Application Data \ Microsoft \ Crypto \ RSA \ MachineKeys. Most likely, your user can create (for example) a text file in this folder, but not delete it, because you do not have full control.

Your options

, . , . http://support.targetprocess.com/Default.aspx?g=posts&t=305.

+7

(.NET-, W2008R2), .

procmon, C:/ProgramData/Microsoft/Crypto/RSA/MachineKeys.

EVERYONE , , ​​: http://toastergremlin.com/?p=432

, , EVERYONE @ , EVERYONE @ !

+4

-, , . , .

, , . , " " Windows, .:-P

() temp .

+1

, . :

  • Castle.Core .
  • . :
  • . :

The following might be a dirty hack, but it works quite well, and I don't need to explain why I want all end-user computers to be reconfigured.

/// <summary>
/// Ensures that NHibernate creates no strong named proxy assemblies.
/// Assumes usage of Castle.DynamicProxy. Needs to be revisited
/// after update of NHibernate or Castle.Proxy!
/// </summary>
private static void EnsureNHibernateCreatesNoStrongNamedProxyAssemblies()
{
    if (!StrongNameUtil.CanStrongNameAssembly)
    {
        Logger.Debug("NHibernate is not trying to strong name assemblies." +
                     "No action needed.");
        return;
    }

    const string FieldName = "canStrongNameAssembly";
    var type = typeof(StrongNameUtil);
    var field = type.GetField(FieldName, BindingFlags.Static
                                         | BindingFlags.NonPublic);
    if (field == null)
    {
        Logger.Warn(
            "No field with the name {0} exists in the type {1}."
            + "Can't change NHibernate to use weak named proxy assemblies.", 
            FieldName, type);
        return;
    }

    field.SetValue(null, false);

    if (StrongNameUtil.CanStrongNameAssembly)
    {
        Logger.Warn(
            "Couldn't change value of field {0} on type {1}. "
            + "NHibernate will continue to use strong named proxy assemblies.", 
            FieldName, type);
    }
    else
        Logger.Debug("Successfully changed NHibernate to use "
                     + "weak named proxy assemblies.");
}

Just make sure you call this method at the very beginning of your program before creating the first proxy.

I guess the real solution would be to upgrade to NHibernate 3.3, which suppedly no longer has this problem, but this is not an option now.

0
source

All Articles