我正在尝试使用ServiceStack创建一个简单的Web服务。在服务器(服务)方面,我首先创建了一个用户“管理员”并为其分配了“管理员”角色。我正在使用ServiceStack内置凭证身份验证,我很好地进行身份验证。但是,对使用[Authenticate]属性修饰的任何Web服务的任何后续调用都会返回以下错误:
没有为OAuth提供商“基本”
添加配置仅供参考:数据库是RavenDb - 使用ServiceStack.Authentication.RavenDb。我还在App_Start.AppHost.Configure()中注册了一个MemoryCacheClient。如果您需要查看App_Start.AppHost类中的所有代码,请告诉我。
服务器代码(摘录):
namespace MyTest
{
[Route("/hello")]
[Route("/hello/{Name}")]
[Authenticate]
public class Hello
{
public string Name { get; set; }
}
public class HelloResponse
{
public string Result { get; set; }
}
public class HelloService : Service
{
public IUserAuthRepository UserAuthRepo { get; set; }
public object Any(Hello request)
{
return new HelloResponse { Result = "Hello, " + request.Name };
}
}
}
客户方:
var client = new JsonServiceClient("http://127.0.0.1:65385/auth/credentials?format=json");
var auth = new Auth { UserName = "administrator", Password = "12345" };
var authResponse = client.Post(auth);
if (authResponse.ResponseStatus.ErrorCode == null)
{
client = new JsonServiceClient("http://127.0.0.1:65385/hello");
client.UserName = "administrator";
client.Password = "12345";
// When sending the request - it returns "Not Found"
var helloResponse = client.Send(new Hello { Name = "John" });
}
编辑:
我的服务的web.config看起来与the Hello World tutorial
以下是我的AppHost的配置:
public override void Configure(Funq.Container container)
{
container.Register(new MemoryCacheClient { FlushOnDispose = false });
container.Register(new EmbeddableDocumentStore { DataDirectory = "Data" }.Initialize());
ConfigureAuth(container);
}
private void ConfigureAuth(Funq.Container container)
{
var appSettings = new AppSettings();
Plugins.Add(new AuthFeature(() => new CustomUserSession(),
new IAuthProvider[] {
new CredentialsAuthProvider(appSettings)}));
Plugins.Add(new RegistrationFeature());
container.Register(
new RavenUserAuthRepository(c.Resolve()));
}
答案 0 :(得分:1)
首先,您应该查看一些使用ServiceStack's AuthTests测试身份验证的示例ServiceStack's C# Service Clients。
您在C#ServiceStack客户端传递了错误的网址,即您只应传递托管ServiceStack的基本URI ,例如:
var client = new JsonServiceClient(
"http://127.0.0.1:65385/auth/credentials?format=json");
var client = new JsonServiceClient("http://127.0.0.1:65385/hello");
应该只是:
var client = new JsonServiceClient("http://127.0.0.1:65385/");
var client = new JsonServiceClient("http://127.0.0.1:65385/");
在使用ServiceStack C#ServiceClient(如JsonServiceClient)时,您也不应该使用?format = json ,因为客户端已隐含格式,以确保通过发送Accept: application/json
来返回JSON每个请求都有HTTP标头。
答案 1 :(得分:1)
好的,找出问题所在:[Authenticate],[RequiredRole]和[RequiredPermission]属性与基本身份验证一起使用(身份验证也适用于摘要身份验证)。因此,如果要使用任何这些属性,则必须确保在将AuthFeature设置为插件时已将BasicAuthProvider添加到IAuthProviders列表中。所以,
Plugins.Add(new AuthFeature(() => new CustomUserSession(),
new IAuthProvider[] {
new CredentialsAuthProvider(appSettings)}));
必须是
Plugins.Add(new AuthFeature(() => new AuthUserSession(),
new IAuthProvider[] {
new CredentialsAuthProvider(appSettings),
new BasicAuthProvider()
}));
我仍然需要在每次通话时以明文形式发送用户名和密码......
答案 2 :(得分:0)
确保在ServiceStack应用程序中注册ICacheClient,然后您不必为每个请求发送用户名和密码。您的客户端应该能够在不费力的情况下处理存储cookie。