小编hen*_*gst的帖子

使用带有ISO-8859-1编码的XmlTextWriter编写XML文件

我在使用C#将挪威字符写入XML文件时遇到问题.我有一个包含一些挪威文字的字符串变量(字母像æøå).

我正在使用XmlTextWriter编写XML,将内容写入MemoryStream,如下所示:

MemoryStream stream = new MemoryStream();
XmlTextWriter xmlTextWriter = new XmlTextWriter(stream, Encoding.GetEncoding("ISO-8859-1"));
xmlTextWriter.Formatting = Formatting.Indented;
xmlTextWriter.WriteStartDocument(); //Start doc
Run Code Online (Sandbox Code Playgroud)

然后我像这样添加我的挪威文字:

xmlTextWriter.WriteCData(myNorwegianText);
Run Code Online (Sandbox Code Playgroud)

然后我将文件写入磁盘,如下所示:

FileStream myFile = new FileStream(myPath, FileMode.Create);
StreamWriter sw = new StreamWriter(myFile);

stream.Position = 0;
StreamReader sr = new StreamReader(stream);
string content = sr.ReadToEnd();

sw.Write(content);
sw.Flush();

myFile.Flush();
myFile.Close();
Run Code Online (Sandbox Code Playgroud)

现在的问题是,在这个文件中,所有的挪威字符看起来都很有趣.

我可能会以某种愚蠢的方式做上述事情.有关如何解决它的任何建议?

.net c# xml encoding iso-8859-1

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

MemoryCache.Default在.NET Core中不可用?

我正在从.NET 4.6到.NET Core移植一些代码,并且遇到了MemoryCache的一些问题.4.6代码使用MemoryCache.Default来实例化缓存,但这似乎在.NET Core中不可用.在.NET Core中是否有相同的内容,或者我是否应该将我自己的MemoryCache作为单例创建并通过IOC注入它?

.net c# memorycache coreclr dnx

13
推荐指数
2
解决办法
9283
查看次数

使用Client Credentials流持久化令牌的最佳实践

我有一个允许匿名用户的ASP.NET核心MVC应用程序.此应用程序调用受Identity Server 4保护的ASP.NET Web API.我在Identity Server中创建了一个描述MVC应用程序(客户端)的客户端,并允许它访问api作用域,如下所示:

new Client
{
    ClientId = "my-mvc-client-app",
    AllowedGrantTypes = GrantTypes.ClientCredentials,

    RequireConsent = false,
    ClientSecrets = new List<Secret> { new Secret("this-is-my-secret".Sha256()) },
    AllowedScopes = new List<string>
    {
        StandardScopes.OpenId.Name,
        StandardScopes.Profile.Name,
        StandardScopes.OfflineAccess.Name,
        "my-protected-api"
    },
    RedirectUris = new List<string>
    {
        "http://localhost:5009/signin-oidc",
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的MVC应用程序中,我正在使用TokenClient一个令牌,我可以在向受保护的API发出请求时使用这样的令牌:

var disco = await DiscoveryClient.GetAsync("http://localhost:5010");
var tokenClient = new TokenClient(disco.TokenEndpoint, clientId, clientSecret);
var tokenResponse = await tokenClient.RequestClientCredentialsAsync("hrmts-test-candidate-api-scope");
Run Code Online (Sandbox Code Playgroud)

这工作正常,但我在每次请求时都要求Identity Server提供新令牌,这可能不是一个好主意.

处理令牌的最佳做法是什么?如何在客户端(MVC应用程序)上保留它们以及如何处理刷新令牌以确保客户端在必要时获取新令牌?

c# identityserver4

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

持久保存到Azure表存储时使用POCO

我打算在我的ASP.NET 5(MVC 6)应用程序中使用Azure表存储并添加了WindowsAzure.StorageNuGet包,但是当我注意到我所有的entant模型都需要继承时,我真的很失望Microsoft.WindowsAzure.Storage.Table.TableEntity.现在我认为最好的解决方案是拥有两组实体并在我的主域对象和用于持久存储到表存储的实体对象之间创建映射.我不想将WindowsAzure.Storage包添加到我的所有项目中.

弃用的azure-sdk-for-net在某一点上得到了对POCO的支持,但我在目前的WindowsAzure.Storage中没有看到这一点.

这里的最佳做法是什么?

c# asp.net azure azure-table-storage asp.net-core

8
推荐指数
2
解决办法
3384
查看次数

在同一个 WebForms 应用程序中组合 Forms auth 和 OpenIdConnect auth

我有一个旧的多租户 WebForms 应用程序,其中用户使用表单身份验证进行身份验证。我们正在将 auth 系统迁移到 IdentityServer4,但不能一次全部完成,因此我们希望逐步将其介绍给我们的租户。这意味着我们需要同时使用 Forms Auth 和新的 OpenIdConnect Auth 运行 WebForms 应用程序。

我的问题是,每当我运行时HttpContext.Current.GetOwinContext().Authentication.Challenge(),我都会被重定向到Login.aspx

<authentication mode="Forms">
  <forms name="AuthCookieName" loginUrl="~/Login.aspx" timeout="60" protection="All" requireSSL="true" enableCrossAppRedirects="true" />
</authentication>
Run Code Online (Sandbox Code Playgroud)

我想要的是,每当有人导航到 时/OIDC.aspx,挑战都会将用户重定向到使用 OWIN 配置的 IdentityServer。对于所有其他请求,现有的 Forms 身份验证配置可以处理身份验证。

这是可能吗?

asp.net authentication webforms openid-connect identityserver4

7
推荐指数
2
解决办法
1942
查看次数

CoreCLR中的Type.InvokeMember(..)

我正在尝试使用CoreCLR动态调用特定类型的成员,但是在针对DNXCORE50进行编译时,方法Type.InvokeMember不可用.但是,如果我针对DNX451编译它可以正常工作.

下面是使用DNX451如何实现这一目标的示例,但我如何在DNXCORE50中执行相同操作?

using System;
using System.Reflection;

namespace InvokeMember
{
    public class Program
    {
        public void Main(string[] args)
        {
            typeof (Program).InvokeMember("DoStuff", BindingFlags.InvokeMethod, null, new Program(), null);
        }

        public void DoStuff()
        {
            Console.WriteLine("Doing stuff");
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

c# reflection coreclr dnx

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

在ASP.NET Core应用程序(RC2)中使用net451库

我正在尝试将我的ASP.NET 5 RC1项目升级到ASP.NET Core RC2项目.我遇到了一些问题,因为我使用的是尚未支持.NET Core的库,所以我必须在完整的框架上运行.这在RC1中运行良好,但我无法找到在RC2中实现这一目标的正确方法.

我有一个类库,可以恢复包并正确构建.我有一个引用类库的测试项目.当我尝试构建测试项目时,我收到以下错误:

> dotnet build
Project TenantService (.NETFramework,Version=v4.5.1) was previously compiled. Skipping compilation.
Project TenantServiceTests (.NETCoreApp,Version=v1.0) will be compiled because expected outputs are missing
Compiling TenantServiceTests for .NETCoreApp,Version=v1.0
C:\projects\TenantService\test\TenantServiceTests\project.json(25,23): error NU1001: The dependency mscorlib could not be resolved.
C:\projects\TenantService\test\TenantServiceTests\project.json(9,31): error NU1001: The dependency mscorlib could not be resolved.
C:\projects\TenantService\test\TenantServiceTests\project.json(25,23): error NU1001: The dependency mscorlib could not be resolved.
C:\projects\TenantService\test\TenantServiceTests\project.json(9,31): error NU1001: The dependency mscorlib could not be resolved.
C:\projects\TenantService\test\TenantServiceTests\project.json(26,21): error NU1001: The dependency System could …
Run Code Online (Sandbox Code Playgroud)

xunit.net project.json asp.net-core .net-core-rc2

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