小编Sam*_*uyn的帖子

在Windows应用商店应用中使用httpclient获取UTF-8响应

我正在构建一个Windows应用商店应用,但我坚持从API获取UTF-8响应.

这是代码:

using (HttpClient client = new HttpClient())
{
    Uri url = new Uri(BaseUrl + "/me/lists");

    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, url);
    request.Headers.Add("Accept", "application/json");
    HttpResponseMessage response = await client.SendRequestAsync(request);
    response.EnsureSuccessStatusCode();

    string responseString = await response.Content.ReadAsStringAsync();

    response.Dispose();
}
Run Code Online (Sandbox Code Playgroud)

reponseString总是包含奇怪的字符应该是像口音é,我试图使用流,但我在一些例子中发现的API无法在Windows RT存在.

编辑:改进代码,仍然是同样的问题.

.net c# utf-8 character-encoding windows-runtime

11
推荐指数
3
解决办法
2万
查看次数

ASP.NET身份的可变cookie路径

我们将多租户MVC应用程序从ASP.NET成员资格提供程序迁移到ASP.NET Identity.

这是我的Startup.Auth.cs(简化):

public partial class Startup
{
    public void ConfigureAuth(IAppBuilder app)
    {
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
        app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);

        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            Provider = new CookieAuthenticationProvider
            {
                OnValidateIdentity =
                    SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, Identity, int>(
                        TimeSpan.FromMinutes(30),
                        (manager, user) =>
                            manager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie),
                        clIdentity => clIdentity.GetUserId<int>())
            }
        });
        app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
}
Run Code Online (Sandbox Code Playgroud)

在我们的多租户应用程序中,每个租户都有自己的"slug"(例如http://example.com/tenant1/http://example.com/tenant2/)

但是,目前,cookie存储在根目录中.这会导致安全问题,因为来自tenant1的用户会自动从tenant2登录网站.

我们如何使CookiePath(在CookieAuthenticationOptions中)变量,以便它根据租户而变化?

cookies owin asp.net-identity

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

如何使用带有CancellationToken的Task包装单个同步操作?

我只有一个同步操作,可能需要很多时间才能完成。该操作的调用者提供了一个CancellationToken符号,并且在令牌被取消后,该操作应立即停止(在这种情况下,取消后的几毫秒内也将起作用)。

如何将其包装在带有CancellationToken的任务中?

我无法更改调用代码或调用本身。

曾经是: LongOperation();

我现在所拥有的: await Task.Run(() => LongOperation(), cancellationToken).ConfigureAwait(false);

显然,这是行不通的,因为您必须在给的操作中轮询令牌Task.Run

.net c# async-await

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