【发布时间】: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