小编dar*_*ffo的帖子

在C#中正确使用JwtTokens

我正在玩JwtTokens并且无法使它们正常工作.我正在使用http://www.nuget.org/packages/System.IdentityModel.Tokens.Jwt/.我知道代码是乱七八糟的,但只是为了展示我正在尝试做的事情.问题是我希望JwtTokenHandler因为生命周期而无法通过验证.

var key = "5A0AB091-3F84-4EC4-B227-0834FCD8B1B4";
var domain = "http://localhost";
var allowedAudience = "http://localhost";
var signatureAlgorithm = "http://www.w3.org/2001/04/xmldsig-more#hmac-sha256";
var digestAlgorithm = "http://www.w3.org/2001/04/xmlenc#sha256";
var issuer = "self";
var securityKey = System.Text.Encoding.Unicode.GetBytes(key);
var inMemorySymmetricSecurityKey = new InMemorySymmetricSecurityKey(securityKey);

var now = DateTime.UtcNow;
var expiry = now.AddSeconds(1);
var tokenHandler = new JwtSecurityTokenHandler();
var claimsList = new List<Claim>()
{
    new Claim(ClaimTypes.Name, "user"),
    new Claim(ClaimTypes.Webpage, allowedAudience),
    new Claim(ClaimTypes.Uri, domain),                
    new Claim(ClaimTypes.Expiration,expiry.Ticks.ToString())
};
var roles = new List<string>() { "admin" };
claimsList.AddRange(roles.Select(role => new Claim(ClaimTypes.Role, role))); …
Run Code Online (Sandbox Code Playgroud)

c# claims jwt

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

ASP .NET Core webapi 在中间件中设置 cookie

我正在尝试在执行操作后设置 cookie,但正在努力使其正常工作。如果我从控制器而不是从中间件设置它,我设法看到了 cookie。我玩过配置的顺序,什么也没有。代码示例来自一个干净的webapi创建的项目,所以如果有人想玩它很简单,只需创建一个空的webapi,添加CookieSet类并将Startup类替换为下面的类(仅添加了cookie策略选项)

这是我的中间件

public class CookieSet
{
    private readonly RequestDelegate _next;

    public CookieSet(RequestDelegate next)
    {
        _next = next;
    }

    public async Task Invoke(HttpContext context)
    {
        await _next.Invoke(context);
        var cookieOptions = new CookieOptions()
        {
            Path = "/",
            Expires = DateTimeOffset.UtcNow.AddHours(1),
            IsEssential = true,
            HttpOnly = false,
            Secure = false,
        };
        context.Response.Cookies.Append("test", "cookie", cookieOptions);
    }
}
Run Code Online (Sandbox Code Playgroud)

我已经添加了 p 分配并检查了执行永远不会到达那里,在 Cookies.Append 行它停止执行,所以有什么事情发生了我无法弄清楚。

这是我的 Startup 课程

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; …
Run Code Online (Sandbox Code Playgroud)

cookies middleware asp.net-core

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

Rebus OneWayClient 如果不存在则创建队列

当配置为单向客户端(RabbitMq 和 Azure)时,我在使用 rebus 发送消息(而不是发布)时遇到错误,因为队列必须存在。

如果在发送消息之前不存在队列,是否可以使用 OneWayClient 创建队列?

rebus

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

标签 统计

asp.net-core ×1

c# ×1

claims ×1

cookies ×1

jwt ×1

middleware ×1

rebus ×1