小编Vis*_*ish的帖子

在处理SymmetricAlgorithm时,"try-finally"阻止与"使用"块

根据System.Security.Cryptography.SymmetricAlgorithmmsdn文档

请注意,在使用派生类时,从安全角度来看,仅在使用完对象后强制执行垃圾回收是不够的.必须在对象上显式调用Clear方法,才能在对象释放之前将其中的任何敏感数据清零.请注意,垃圾收集不会将收集的对象的内容清零,而只是将内存标记为可用于重新分配.因此,垃圾收集对象中包含的数据可能仍然存在于未分配的存储器中的存储器堆中.对于加密对象,此数据可能包含敏感信息,如密钥数据或纯文本块.

.NET Framework中包含敏感数据的所有加密类都实现了Clear方法.调用时,Clear方法用零覆盖对象中的所有敏感数据,然后释放该对象,以便可以安全地进行垃圾回收.当对象已归零并释放时,您应该调用Dispose方法并将disposing参数设置为True以处置与该对象关联的所有托管和非托管资源.

我从中获得的是我必须使用try-finally块来处理我的算法,如下所示:

SymmetricAlgorithm symmetricAlgorithm = SymmetricAlgorithm.Create()
try 
{
     //stuff
}
finally
{
    symmetricAlgorithm.Clear();
    symmetricAlgorithm.Dispose(true)
}
Run Code Online (Sandbox Code Playgroud)

我无法使用更简洁的using

using (var symmetricAlgorithm = SymmetricAlgorithm.Create())
{
    //do stuff
}
Run Code Online (Sandbox Code Playgroud)

因为它不会清除记忆.它只会标记为收藏.那是对的吗?谢谢您的帮助.

.net c# encryption

5
推荐指数
2
解决办法
2869
查看次数

Linq to NHibernate 查询比较枚举映射为整数成功,但作为等效条件查询失败

我正在查询 ProductRisk,它包含一个 Status 属性,其中 Status 是一个枚举。以下是 ProductRisk 的映射:

public class ProductRiskMap : ClassMap<ProductRisk>
{
    public ProductRiskMap()
    {
        Table("AccountManagement.dbo.ProductRisk");

        Id(x => x.Id, "ProductRiskID");

        References(x => x.AssociatedProduct, "ProductID");
        References(x => x.AssociatedClient, "EntityId");

        Map(x => x.Reason, "ProductRiskReasonID").CustomType<int>();
        Map(x => x.Status, "RiskStatusID").CustomType<int>();
    }
Run Code Online (Sandbox Code Playgroud)

Status 是一个具有四个可能值的枚举。它在数据库中表示为对查找表的外键引用。在我的存储库中,我想提取状态为Medium或的 ProductRisk 对象High。Ling To NHibernate 中的以下查询有效:

    public IList<ProductRisk> GetByClient(int[] clientIds)
    {
        return NHibernateSession.Current.Query<ProductRisk>()
            .Where(x => clientIds.Contains(x.AssociatedClient.Id))
            .Where(x => x.Status == RiskStatus.Medium || x.Status == RiskStatus.High)                
            .ToList();
    }
Run Code Online (Sandbox Code Playgroud)

但是,如果我在 Criteria API 中使用(我认为是)等效的查询:

        return NHibernateSession.Current.QueryOver<ProductRisk>()
            .WhereRestrictionOn(x => x.AssociatedClient.Id).IsIn(clientIds) …
Run Code Online (Sandbox Code Playgroud)

nhibernate linq-to-nhibernate nhibernate-criteria

5
推荐指数
1
解决办法
1182
查看次数

从ItemsControl派生的WPF自定义控件无法显示绑定数据

我创建了一个名为MovableItemsControl的自定义控件,继承自ItemsControl,以覆盖GetContainerForItemOverride()方法.我的问题是绑定集合中没有任何对象正在显示.目前,我绑定了一个OberservableCollection字符串,当我查看调试器时,我可以看到它们在ItemsSource中.

自定义控件如下所示:

public class MovableItemsControl : ItemsControl
{
    static MovableItemsControl()
    {          
        DefaultStyleKeyProperty.OverrideMetadata(typeof(MovableItemsControl), new FrameworkPropertyMetadata(typeof(MovableItemsControl)));
    }

    /// <summary>
    /// Wraps each content object added to the ItemsControl in a NodeWrapper
    /// </summary>
    protected override DependencyObject GetContainerForItemOverride()
    {
        NodeWrapper nodeWrapper = new NodeWrapper();            
        return nodeWrapper;
    }

    protected override bool IsItemItsOwnContainerOverride(object item)
    {
        return item is NodeWrapper;
    }
}
Run Code Online (Sandbox Code Playgroud)

NodeWrapper是一个UserControl,由一个派生自Thumb(MoveThumb)的自定义控件和一个Label(Label仅用于测试)组成.

    <Style TargetType="{x:Type local:MovableItemsControl}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type local:MovableItemsControl}">
                <Border Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}">
                </Border>
            </ControlTemplate>
        </Setter.Value> …
Run Code Online (Sandbox Code Playgroud)

wpf binding wpf-controls

3
推荐指数
1
解决办法
2053
查看次数

使用实体框架查询多个表的联合结果

我想查询 3 个表并抓取所有表的最新活动,由 CreatedDate 时间戳确定。每个表都由我的域模型中的一个实体表示,我使用实体框架代码优先(无数据迁移)来映射我的域。

我知道查询在 SQL 中应该是什么样子,但我不确定如何在 LinqToEntities 或 EntitySql 中制作它。您能否告诉我如何在实体框架中执行此操作,即使使用实体框架查询方法执行此查询是合适的?

在此先感谢您的帮助。

这是我的实体(主键在基类上):

public class Group : EntityThatBelongsToApartmentComplex<int>
{
    public string GroupName { get; set; }
    public string GroupDescription { get; set; }

    [Required]
    public DateTimeOffset CreatedDate { get; protected set; }

    public int CreatedById { get; set; }
    public virtual UserProfile CreatedBy { get; set; }    
}


public class Activity : EntityThatBelongsToApartmentComplex<int>
{
    [StringLength(150)]
    public string Name { get; set; }

    [StringLength(150)]
    public string Description { get; set; …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework

2
推荐指数
1
解决办法
3525
查看次数

将变量赋值给lambda表达式的结果

我想完成以下内容:

(clientSinceChoices =  Enumerable.Range(1949, DateTime.Now.Year - 1950)
    .Select(x => new SelectListItem() 
    { 
        Text = x != 1949 ? x.ToString() : "Unselected", 
        Value = x != 1949 ? new DateTime(x, 1, 1).ToString() : null,
        Selected = () => 
        {
            if (x == 1949 && !ClientSinceYearOnly.HasValue)
                return true;
            else if (ClientSinceYearOnly.Value == x)
                return true;
            else
                return false;
        }
    }));
Run Code Online (Sandbox Code Playgroud)

我希望值Selected是内联定义的labmda表达式的结果.我知道我可以通过将lambda分配给变量然后调用它来实现这一点,但我认为定义并立即调用它是"更干净".

c# lambda

1
推荐指数
1
解决办法
3175
查看次数