假设我已经从客户端发送自定义身份验证数据,并从服务堆栈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?
答案 0 :(得分:2)
使用SetCredentials
为每个客户端请求传递身份验证信息。
var client = new JsonServiceClient("http://somewhere/API");
client.SetCredentials("myuser","password123");
var response = client.Get<ResponseDTO>();