如何在Servicestack Web API中进行身份验证并访问[Authenticate]过滤器

时间:2013-09-19 06:11:04

标签: asp.net-mvc authentication servicestack

假设我已经从客户端发送自定义身份验证数据,并从服务堆栈Web API检索ss-id。

var client = new JsonServiceClient("http://somewhere/API");
var response = client.Post(new Auth() {UserName = "myuser", Password = "password123"});

然后我尝试使用[Authenticate]访问另一个API函数,但它始终失败,401未授权。

public class UsersServices : Service
{
    public UsersRepository Repository { get; set; }

    //cannot access this...
    [Authenticate]
    public object Get(Users user)
    {
        return RequestContext.ToOptimizedResultUsingCache(
            base.Cache, UrnId.Create<Users>("requestAll"), () =>
            {
                var data = Repository.GetAllUsers();
                return data;
            });
    }
}

验证后如何在客户端访问API?

1 个答案:

答案 0 :(得分:2)

使用SetCredentials为每个客户端请求传递身份验证信息。

var client = new JsonServiceClient("http://somewhere/API");
client.SetCredentials("myuser","password123");
var response = client.Get<ResponseDTO>();