小编Lau*_*oun的帖子

完整.NET和.NET Core之间的WCF基本身份验证

我想使用基本身份验证在.NET核心应用程序和完整的.NET Web服务(托管在IIS中)之间建立WCF https连接。

在.NET Core端,我有一个ChannelFactory设置如下:

var binding = new BasicHttpBinding();
binding.Security.Mode = BasicHttpSecurityMode.Transport;
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;

factory = new ChannelFactory<T>(binding, new EndpointAddress(endpointAddress));
factory.Credentials.UserName.UserName = "abc";
factory.Credentials.UserName.Password = "xyz";
Run Code Online (Sandbox Code Playgroud)

在Web服务上,我有一个使用绑定配置的端点,如下所示:

<basicHttpBinding>
    <binding name="BasicHttpBinding" maxReceivedMessageSize="10485760">
      <security mode="Transport">
        <transport clientCredentialType="Basic" proxyCredentialType="None" realm=""/>
      </security>
    </binding>
  </basicHttpBinding>
Run Code Online (Sandbox Code Playgroud)

但是在运行时,我收到此错误消息:

System.ServiceModel.Security.MessageSecurityException: 'HTTP请求未经客户端身份验证方案'Basic'授权。从服务器收到的身份验证标头是“ Basic realm =“ localhost””。

我希望双方都说认证方案为“基本”,并且可以毫无问题地进行连接。我不知道领域来自何处,但我无法摆脱它。我认为这是造成问题的原因。

c# wcf wcf-binding .net-core

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

使用 Linq 来展平嵌套列表而不是 foreach 循环

我有一个经常性的课程 - 我在下面简化了它。

public class SiloNode
{
    public string Key { get; private set; }
    public string Url { get; private set; }
    public List<SiloNode> Children { get; private set; }
}
Run Code Online (Sandbox Code Playgroud)

尽管从理论上讲,它可以永远嵌套,但节点只会向下两层。因此,顶级节点可以有子节点,但子节点不能有子节点。

我有一个主列表,其中包含所有顶级节点及其嵌套子节点。

但是,我需要将所有节点放入一个平面列表中 - 节点 1,然后是它的子节点,然后是节点 2 等。

我在这方面的知识有限,但我可以做一些事情,比如foreach遍历主列表并创建一个新列表,如下所示:

public IEnumerable<SiloNode> GetLinks(IEnumerable<SiloNode> masterList)
{
    var newList = new List<SiloNode>();

    foreach (var node in masterList)
    {
        newList.Add(node);
        newList.AddRange(node.Children);
    }

    return newList;
}
Run Code Online (Sandbox Code Playgroud)

但是,我知道可能有更好的方法,但我只是不知道如何将其转换foreachLinq执行相同操作的语句。换句话说,一起选择父项及其子项。

任何帮助表示赞赏。

c# linq asp.net

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

System.Data.Entity.DbContext 找不到添加为引用

当我去构建我的项目时,我收到此错误:

类型“System.Data.Entity.DbContext”是在未引用的程序集中定义的。您必须添加对程序集“EntityFramework,Version=5.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089”的引用。

我正在尝试访问我DbContext使用 EF5 创建的内容。我尝试添加System.Data.Entity.DbContext到我的参考文献中,但找不到。我应该怎么办?

c# entity-framework

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

Git:以下未跟踪的工作树文件将被合并覆盖:upload / .DS_Store

我是git新手,遇到烦人的错误。我花了几个小时在StackOverflow上浏览不同的帖子,以了解其他用户如何解决此问题,但对我来说没有任何用处。

从仓库中提取时,出现以下错误:

错误:以下未跟踪的工作树文件将被合并覆盖:upload / .DS_Store

  • 我从Mac删除了.DS_Store,但它只是重新创建了文件。
  • 我尝试了git add .git stashgit pull仍然出现错误。
  • 我添加了.DS_Store .gitignore,但是仍然出现错误。

我不知道该怎么做。

git

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

C#REST API调用 - 在Postman中工作,而不是在Code中

我有一些现有的代码正在工作,并且突然之间退出.

我无法弄清楚为什么......

这是我的代码:

public static string RequestToken(string u, string pw)
{
    string result = string.Empty;

    string strUrl = "https://xxx.cloudforce.com/services/oauth2/token?grant_type=password&client_id=XXXX&client_secret=XXXX&username=" + u + "&password=" + pw;
    HttpWebRequest tokenRequest = WebRequest.Create(strUrl) as HttpWebRequest;
    Debug.Print(strUrl);
    tokenRequest.Method = "POST";
    try
    {
        using (HttpWebResponse tokenResponse = tokenRequest.GetResponse() as HttpWebResponse)
        {
            if (tokenResponse.StatusCode != HttpStatusCode.OK)
                throw new Exception(String.Format(
                    "Server error (HTTP {0}: {1}).",
                    tokenResponse.StatusCode,
                    tokenResponse.StatusDescription));
            DataContractJsonSerializer jsonSerializer2 = new DataContractJsonSerializer(typeof(ResponseAuthentication));
            object objTokenResponse = jsonSerializer2.ReadObject(tokenResponse.GetResponseStream());
            ResponseAuthentication jsonResponseAuthentication = objTokenResponse as ResponseAuthentication;
            result = jsonResponseAuthentication.strAccessToken;
        }
    }
    catch …
Run Code Online (Sandbox Code Playgroud)

c# rest salesforce httpwebrequest

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

如何在ASP.NET Core 2.1中全局格式化NodaTime日期字符串?

目前,我正在尝试使用JsonFormatters序列化ISO 8601规范中的字符串.在我的启动配置中格式化,但无法使其工作.

我的启动配置如下:

services.AddMvcCore(
    (options) => {
        options.SslPort = 44321;
        options.Filters.Add(new RequireHttpsAttribute());
    }
)
.AddJsonFormatters(jsonSerializerSettings => {
    jsonSerializerSettings.DateParseHandling = DateParseHandling.None;
    jsonSerializerSettings.DateFormatString = "yyyy-MM-ddTHH:mm:ss.fffZ";
})
.AddApiExplorer()
.AddJsonOptions(options => {
    options.AllowInputFormatterExceptionMessages = false;
    options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
})
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
.AddDataAnnotationsLocalization();
Run Code Online (Sandbox Code Playgroud)

我也尝试过文档中提到的ServiceStackText,但这也没有用.

 NodaSerializerDefinitions.LocalTimeSerializer.ConfigureSerializer();
 DateTimeZoneProviders.Tzdb
     .CreateDefaultSerializersForNodaTime()
     .ConfigureSerializersForNodaTime();
Run Code Online (Sandbox Code Playgroud)

我一直得到以下格式,

LocalDate序列化:

{
    "patientDob": "Thursday, June 15, 2017",
}
Run Code Online (Sandbox Code Playgroud)

如何配置字符串ISO 8601规范.NodaTime全局格式化日期类型?

我的模特,

{
    public LocalDate patientDob { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

和我的视图模型/ API资源: …

nodatime json-serialization asp.net-core-2.1

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

为什么我的 EF 似乎从正在运行的 SQL 视图中返回重复的行?

我查过这个问题,但我发现没有任何东西对我有用。我在 SQL 中创建了一个视图,当您在 Management Studio 中运行它时该视图有效。当从我的 MVC 应用程序访问视图时,EF 返回相同的行而不是具有不同数据的行。

表:汽车

  • [ID]
  • [登记]
  • [制作]
  • [模型]

表:预订

  • [ID]
  • [预订开始日期]
  • [预订结束日期]
  • [车号]

查看:CarBookings

SELECT  [C].[Id],
        [C].[Registration],
        [C].[Make],
        [C].[Model],
        [B].[BookingStartDate],
        [B].[BookingEndDate]

FROM [Cars] AS C INNER JOIN [Bookings] AS B ON C.Id = B.CarId
Run Code Online (Sandbox Code Playgroud)

如果我在 SSMS 中运行查询,我会得到所有预期的结果,例如:

  • 汽车 1, 预订 12/03/2018
  • 汽车 1, 预订 19/09/2018

当我从我的 MVC 应用程序访问相同的视图时,我得到:

  • 汽车 1, 预订 12/03/2018
  • 汽车 1, 预订 12/03/2018

在控制器上放置一个断点表明结果是相同的,所以它不是导致它的表示层。没有应用过滤器,也没有任何条件。

我正在使用KendoUI并将结果返回到Grid.

这是我用于获取数据的控制器代码:

家庭控制器.cs

public ActionResult GetBookings([DataSourceRequest] DataSourceRequest request)
{
    var bookings = unitOfWork.BookingsRepository.Get(); …
Run Code Online (Sandbox Code Playgroud)

c# sql entity-framework

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

从 ObjectResult 获取值

我有一个像这样的过滤器:

public class Err : ActionFilterAttribute
{
    public override void OnActionExecuted(ActionExecutedContext context)
    {
        var result = context.Result;
    }
}
Run Code Online (Sandbox Code Playgroud)

result是 的一个对象Microsoft.AspNetCore.Mvc.BadRequestObjectResult。它包含 aStatusCode和 a Value,但是当我尝试像这样提取它们时:context.Result.Value,我收到此错误:

错误 CS1061“IActionResult”不包含“Value”的定义,并且找不到接受“IActionResult”类型的第一个参数的可访问扩展方法“Value”。

c# asp.net-core

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

与lambda表达的字典

很抱歉要问,但我似乎无法找到下一个代码的lambda表达式:

private void SelectIdCeoButton_Click(object sender, EventArgs e)
{
      int id = Convert.ToInt32(this.selectedIdCeo.Text);
      string name = "";

      if (id < 1 || id > 10)
      {
          throw new ArgumentException();
      }
      else
      {
          foreach (var line in ceoExtra.GiveCeoInfo())
          {
              CeoDiscription ceoDis = line.Value;

              if (id.Equals(ceoDis.id))
              {
                  name = ceoDis.ceoName.ToString();
              }
          }
          this.infoBox.Items.Add("chosen ceo: " + name);
      }
  }
Run Code Online (Sandbox Code Playgroud)

这是内部foreach代码块.

我尝试使用lambda表达式进行了研究,但它总是给出错误.它工作正常,但我需要找到它的lambda表达式.

我做了一个枚举列表CEO的,如果我给ID我的接口,它需要返回CEOID.以下是我使用的其他类:

class CeoDiscriptionExtra : FactoryClass
{
    CeoDiscription ceo1 = new CeoDiscription(1, CEO.Bill_Gates, …
Run Code Online (Sandbox Code Playgroud)

c# lambda dictionary

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

将 System.IO.Abstractions 与 DirectoryInfo 结合使用

如何使用 Moq for 编写单元测试DirectoryInfo?我的班级如下:

public class DataProcessor : IDataProcessor, IDisposable
{
    private ILogger _logger;
        
    DataProcessor(ILogger logger)
    {
        _logger = logger;
    }

    public async Task Run(string filePath)
    {
        var dir = new DirectoryInfo( filePath);
        var filesInDir = dir.GetFiles("*.xml");

        foreach(var filePath in filesInDir)
        {
            // process file
        }   
    }
}
Run Code Online (Sandbox Code Playgroud)

我尝试使用 System.IO.Abstractions 并按如下方式更改了我的类,但DirectoryInfo不适用于 System.IO.Abstractions

public class DataProcessor : IDataProcessor, IDisposable
{
    private readonly IFileSystem _fileSystem;

    private ILogger _logger;
        
    DataProcessor(ILogger logger) : this(new FileSystem())
    {
        _logger = logger;
    } …
Run Code Online (Sandbox Code Playgroud)

c# moq

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