经过大量的努力(以及许多tuturials,指南等)后,我设法设置了一个小的.NET Core REST Web API,当存储的用户名和密码有效时,Auth Controller会发出JWT令牌.
令牌将用户ID存储为子声明.
当方法使用Authorize注释时,我还设法设置Web API来验证这些令牌.
app.UseJwtBearerAuthentication(...)
现在我的问题是:如何在我的控制器中(在Web API中)读取用户ID(存储在主题声明中)?
基本上这个问题(我如何在ASP .NET Core中获得当前用户)但我需要一个web api的答案.我没有UserManager.所以我需要从某个地方阅读主题索赔.
有没有办法用.NET Core开发一个Windows服务(能够启动等...)?
我找到的所有教程和说明都使用System.ServiceProcess.ServiceBase,在Visual Studio 2015中由于某种原因无法找到和添加?
我也尽量避免使用像SrvStart这样的第三方工具/库.像Topshelf这样的东西是可以接受的,但似乎不适用于.NET核心.
如果服务可以在windows和linux下运行,那将会很棒.
有什么想法我能做到这一点吗?
我正在努力实现(或理解)JWT承载令牌认证的签名密钥.我希望有人可以帮助我或解释我的错误.
在过去的几周里,我抓了大量的教程,设法让一个自定义的Auth-Controller运行,它发出我的令牌并设法建立JWT承载认证来验证标题中的令牌.
有用.
我的问题是所有示例和教程要么生成随机或内存(发行者)签名密钥,要么使用硬编码的"密码"字符串或从某些配置文件中获取它们(在代码示例中查找"密码").
我的意思是验证设置(在StartUp.cs中):
  //using hardcoded "password"
  SecurityKey key = new SymmetricSecurityKey(System.Text.Encoding.ASCII.GetBytes("password"));
  app.UseJwtBearerAuthentication(new JwtBearerOptions
  {
    AutomaticAuthenticate = true,
    AutomaticChallenge = true,
    TokenValidationParameters = new TokenValidationParameters
    {
      ValidateIssuer = true,
      ValidIssuer = "MyIssuer",
      ValidateAudience = true,
      ValidAudience = "MyAudience",
      ValidateLifetime = true,
      IssuerSigningKey = key
    }
  });
Run Code Online (Sandbox Code Playgroud)
在创建令牌的AuthController中:
  //using hardcoded password
  var signingKey = new SymmetricSecurityKey(System.Text.Encoding.ASCII.GetBytes("password"));
  SigningCredentials credentials = new SigningCredentials(signingKey, SecurityAlgorithms.HmacSha256);
  var jwt = new JwtSecurityToken     // Create the JWT and write it to a string
  (
    issuer: _jwtTokenSettings.Issuer, …Run Code Online (Sandbox Code Playgroud) 在两个项目(.NET Core Web API和.NET Core WindowsService)中,我使用appsettings.json进行配置.
 var configuration = new ConfigurationBuilder()
           .SetBasePath(System.IO.Directory.GetCurrentDirectory())
           .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
           .AddEnvironmentVariables()
           .Build();
Run Code Online (Sandbox Code Playgroud)
在两者中,我将reloadOnChange设置为true并使用它作为IOptions依赖注入注入.在web api中进入控制器类,在服务内进入使用设置的类.
不幸的是,我发现appsettings.json更改时值不会改变.
在web api上,我创建了一个控制器,只是从配置中返回一个字符串值,这与启动时保持一致.
所以我的问题:
我正在使用XSLT将XML转换为HTML.
我有以下XML结构:
<root>
    <element>
        <subelement>
            This is some html text which should be <span class="highlight">displayed highlighted</span>.
         </subelement>
    </element>
</root>
Run Code Online (Sandbox Code Playgroud)
我使用以下模板进行转换:
<xsl:template name="subelement">
  <xsl:value-of select="." />
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
不幸的是,我失去了<span>-tags.
有没有办法保留它们以便正确显示HTML(突出显示)?
我正在尝试使用ASP.NET Core和EntityFramework Core在Linq中使用左连接.
两个表的简单情况:
我尝试查询的数据是Person.id,Person.firstname,Person.lastname和PersonDetails.DetailText.有些人没有DetailText,所以想要的结果是NULL.
在SQL中它工作正常
SELECT p.id, p.Firstname, p.Lastname, d.DetailText FROM Person p 
LEFT JOIN PersonDetails d on d.id = p.Id 
ORDER BY p.id ASC
Run Code Online (Sandbox Code Playgroud)
结果如预期:
# | id | firstname | lastname | detailtext
1 | 1  | First1    | Last1    | details1
2 | 2  | First2    | Last2    | details2
3 | 3  | First3    | Last3    | NULL
Run Code Online (Sandbox Code Playgroud)
在我的Web API控制器中我查询:
[HttpGet]
public IActionResult Get()
{
    var result = from person in _dbContext.Person
                    join …Run Code Online (Sandbox Code Playgroud) 解释为什么这个问题不同于:EF - 多个包括急切加载分层数据.不好的做法?
在当前项目(.NET核心web api)中,我尝试从自引用表中加载层次结构.
经过谷歌搜索后,我很惊讶这样的任务(我认为这将是微不足道的)似乎并不是微不足道的.
好吧,我有这个表来形成我的层次结构:
CREATE TABLE [dbo].[Hierarchy] (
    [Id]        INT           IDENTITY (1, 1) NOT NULL,
    [Parent_Id] INT           NULL,
    [Name]      NVARCHAR (50) NOT NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC),
    CONSTRAINT [FK_Hierarchy_Hierarchy] FOREIGN KEY ([Parent_Id]) REFERENCES [dbo].[Hierarchy] ([Id])
);
Run Code Online (Sandbox Code Playgroud)
在web api中,我尝试返回完整的层次结构.一个可能特别的事情(可能会有所帮助)就是我要加载完整的表格.
我也知道我可以使用预先加载和导航属性(子项为Parent和InverseParent)
_dbContext.Hierarchy.Include(h => h.InverseParent).ThenInclude(h => h.InverseParent)...
Run Code Online (Sandbox Code Playgroud)
问题是这会加载硬编码深度(例如,如果我使用1 Include()和5 ThenInclude(),则为六个级别),但我的层次结构具有灵活的深度.
任何人都可以通过给我一些代码来帮助我,如何加载整个表(例如,在1 DB调用的最佳方案中进入内存),然后使该方法返回完整的层次结构?
在我的项目中,我经常在日志消息中添加前缀。
目前我正在这样做
      logger.LogDebug(prefix + " some message");
Run Code Online (Sandbox Code Playgroud)
我认为这将是实现自定义记录器的好方法,我在其中设置前缀,并且记录器本身在每次记录某些内容时都会附加它。
所以我创建了我的自定义记录器类并实现了 ILogger 接口。但我不明白如何使用
    public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
Run Code Online (Sandbox Code Playgroud)
添加前缀的方法(它是自定义记录器类的成员)。
我的完整代码是:
      public class CustomLogger : ILogger
      {
        private readonly ILogger _logger;
        private string _logPrefix;
        public CustomLogger(ILogger logger)
        {
          _logger = logger ?? throw new ArgumentNullException(nameof(logger));
        _logPrefix = null;
        }
        public ILogger SetLogPrefix(string logPrefix)
        {
          _logPrefix = logPrefix;
          return this;
        }
        public IDisposable BeginScope<TState>(TState state)
        {
          return _logger.BeginScope(state);
        }
        public bool IsEnabled(LogLevel logLevel)
        {
          return …Run Code Online (Sandbox Code Playgroud) In a project I need to configure some third party library via the Micorosoft.Extensions.Configuration.
The library gives an options class and I used the configurationSection.Bind(optionsClassInstance) method to bind the values.
It works well except the nested TimeSpan value. I can't figure out what the json structure of a timespan is so it could be bound.
There are no errors. The values from the json are simply not bound.
So far I just used "timespan": { "Days": 0, "Hours": 1, "Minutes": …
如何获取包含有关已用磁盘空间和空闲/可用磁盘空间信息的存储统计信息。使用针对 Minio 服务器的 minio dotnet 客户端?
否则:复制文件时如何检测存储空间问题?