我正在尝试使用 ServiceStack.net,所以我的第一个服务已经实现了一个 Custom CredentialsAuthProvider,它的 TryAuthenticate 方法现在只返回 True。
我遇到的问题是客户首先调用(并成功)
var rc = new JsonServiceClient("http://localhost:1337/");
var ar = rc.Send<AuthResponse>(new Auth
{
provider = CredentialsAuthProvider.Name,
UserName = "user",
Password = "p@55word",
RememberMe = true
});
其次是:
var newJob = rc.Post<BladeJob>("/jobs", new BladeJob { Name = "TestJob" }); //todo.Id =
它只是返回“未经授权”(异常)。
服务很简单:
[Authenticate()]
public class BladesService : RestServiceBase<BladeJob>
{
public JobRepository Repository { get; set; }
public override object OnPut(BladeJob request)
{
return Repository.Store(request);
}
public override object OnPost(BladeJob job)
{
return Repository.Store(job);
}
public override object OnGet(BladeJob request)
{
IAuthSession session = this.GetSession();
if (request.JobID != default(ulong))
return Repository.GetByID(request.JobID);
return Repository.GetAll();
}
}
我在 TryAuthenticate 中放了一个断点,它就很好地命中了,我做错了什么?
整个主机配置在这里:
public override void Configure(Funq.Container container)
{
//Register Web Service dependencies
container.Register(new JobRepository());
//Register all Authentication methods you want to enable for this web app.
Plugins.Add(new AuthFeature(() => new AuthUserSession(),
new AuthProvider[] {
new CustomCredentialsAuthProvider() //HTML Form post of UserName/Password credentials
}
));
//Register user-defined REST-ful routes
Routes
.Add<BladeJob>("/jobs")
.Add<BladeJob>("/jobs/{JobID}");