小编hol*_*hen的帖子

SslStream:处理证书时发生未知错误

我正在尝试使用 SslStream 和 TLS 1.2 协议建立到远程服务器的 TCP 连接。代码如下:

_tcpClient.Connect(endPoint);

var certificate = new X509Certificate2(_settings.CertificateFilePath, _settings.CertificatePassword, X509KeyStorageFlags.MachineKeySet);
var certificates = new X509CertificateCollection { certificate };

_nStream = _tcpClient.GetStream();
_sslStream = new SslStream(_nStream, false,
    (o, x509Certificate, chain, errors) => true,
    (o, s, collection, x509Certificate, issuers) =>
    { return collection[0]; }
);

_sslStream.AuthenticateAsClient(_settings.HostIpAddress, certificates, SslProtocols.Tls12, true);
_sslStream.Write(someData, 0, someData.Length);
Run Code Online (Sandbox Code Playgroud)

但是,我遇到了一个例外:

System.Security.Authentication.AuthenticationException:对 SSPI 的调用失败,请参阅内部异常。---> System.ComponentModel.Win32Exception: 处理证书时发生未知错误

--- 内部异常堆栈跟踪结束

在 System.Net.Security.SslState.CheckThrow(Boolean authSucessCheck) 在 System.Net.Security.SslStream.Write(Byte[] buffer, Int32 offset, Int32 count)

我启用了 SChannel 日志记录并在 Windows 事件日志中找到了这个:

远程服务器已请求 …

c# ssl sslstream tls1.2

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

ASP.NET MVC - 正确使用View Model和Command模式

我已经编写ASP.NET MVC应用程序一段时间了,我发现它们是使用命令模式的好地方:我们将每个用户请求表示为命令 - 一组输入参数 - 然后处理此命令(处理包括验证和其他域逻辑),结果将发送回用户.

我在我的应用程序中使用的另一件事是视图模型.我发现它们是将数据传递给视图比使用域对象作为模型或填充ViewData/ViewBag更方便的方式.

这两个概念非常适合将显示给用户的数据与用户输入及其处理分开,但它们在ASP.NET MVC中并不完全一致.

假设我想在开发一个简单的网上商店时使用命令和视图模型,用户可以在其中查看产品,并可以通过提供产品名称和电子邮件地址来订购产品:

class ProductViewModel 
{
    public ProductViewModel(int id) { /* init */ }
    public int Id { get; set; }
    public string Name { get; set; }
    // a LOT of other properties (let's say 50)
}

class OrderProductCommand
{
    public int ProductId { get; set; }

    [Required(ErrorMessage = "Name not specified")]
    public string Name { get; set; }

    [Required(ErrorMessage ="E-Mail not specified")]
    public string Email { get; set; }

    public CommandResult …
Run Code Online (Sandbox Code Playgroud)

asp.net-mvc design-patterns command-pattern

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

闭包在for循环和foreach循环中表现不同

在C#中尝试使用闭包时,我发现如果它们在循环中捕获迭代器变量,它们会出乎意料地工作.

var actions = new List<Action>();

foreach (int i in new[] { 1, 2 })
    actions.Add(() => Console.WriteLine(i));

for (int i = 3; i <= 4; i++)
    actions.Add(() => Console.WriteLine(i));

foreach (var action in actions)
    action();
Run Code Online (Sandbox Code Playgroud)

上面的代码产生了一个奇怪的结果(我使用的是.NET 4.5编译器):

1
2
5
5
Run Code Online (Sandbox Code Playgroud)

为什么i两个几乎相同的循环捕获的值不同?

c# closures for-loop

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

NHibernate - 如何解决SQL Server中的参数计数限制

我有一个网站,以简单的表格形式显示SQL Server的数据,包括过滤器,排序,页面导航等.我使用Fluent NHibernate作为ORM,查询代码如下所示:

public IList<Operation> GetResults(UserCommand command)
{
    var result = Session.Query<Operation>();

    if (command.Ids != null)
        result = result.WhereRestrictionOn(o => o.Id).IsIn(command.Ids);

    // ... other filters ...

    return result.Skip(command.Page * command.PageSize).Take(command.PageSize).List();        
}
Run Code Online (Sandbox Code Playgroud)

问题是command.Ids(和一些其他输入参数)可能包含很多值.如果数量超过2100,则查询执行失败,并显示以下错误

System.Data.SqlClient.SqlException: The incoming request has too many parameters. The server supports a maximum of 2100 parameters. Reduce the number of parameters and resend the request.
Run Code Online (Sandbox Code Playgroud)

将查询成小块具有小于2100点的参数是不是一种选择,因为有多个查询我就没有多少记录知道该方式Skip,并Take以显示所需的页面.

还有其他减少参数数量的解决方法吗?

c# sql-server nhibernate fluent-nhibernate

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