【问题标题】:Use cookie for authentication in web api 2在 web api 2 中使用 cookie 进行身份验证
【发布时间】:2014-07-31 07:21:36
【问题描述】:

我正在使用 Thinklecture 库在 Web api 中实现身份验证以进行 WebApi 身份验证。使用以下代码在 url 中使用 accesstoken 可以正常工作:

var config = new AuthenticationConfiguration();

// for testing
config.RequireSsl = false;

// security by user token
var handler = new SimpleSecurityTokenHandler("my access key", token =>
{
    if (ObfuscatingComparer.IsEqual(token, "accesskey123"))
    {
        return new ClaimsPrincipal(new ClaimsIdentity(new Claim[]
        {
            new Claim("customerid", "123")
        }, "Custom"));
    }

    return null;
});

config.AddAccessKey(handler, AuthenticationOptions.ForQueryString("userToken"));

这里,AuthenticationConfiguration 是 Thinklecture 库的一部分。

当我尝试通过以下代码使用 cookie 进行身份验证时,我没有开始工作。我已经在 fiddler 中验证了 cookie 发送正确。

var handler2 = new SimpleSecurityTokenHandler("my access key", token =>
{
    if (ObfuscatingComparer.IsEqual(token, "accesskey123"))
    {
        return new ClaimsPrincipal(new ClaimsIdentity(new Claim[]
        {
            new Claim("customerid", "123")
        }, "Custom"));
    }

    return null;
});

config.AddAccessKey(handler2, AuthenticationOptions.ForCookie("company.userToken"));

我是不是用错了这个库?

【问题讨论】:

    标签: c# authentication cookies asp.net-web-api asp.net-web-api2


    【解决方案1】:

    cookie 选项未实现。

    【讨论】:

      猜你喜欢
      • 2015-01-12
      • 1970-01-01
      • 2016-06-17
      • 1970-01-01
      • 2017-06-02
      • 1970-01-01
      • 2013-11-29
      • 2014-01-28
      • 1970-01-01
      相关资源
      最近更新 更多