在web api 2中使用cookie进行身份验证

时间:2014-06-10 13:37:38

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

我正在使用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进行身份验证时使用以下代码。我已经在提琴手中验证了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"));

我使用这个库错了吗?

1 个答案:

答案 0 :(得分:-1)

未实施Cookie选项。

相关问题