簡體   English   中英

在Web API 2中使用Cookie進行身份驗證

[英]Use cookie for authentication in web api 2

我正在使用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"));

我使用這個庫錯了嗎?

cookie選項未實現。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM