使用自定义服务栈 AuthProvider
Consume custom servicestack AuthProvider
我正在尝试从 ServerEventsClient 访问我的自定义 AuthProvider(继承自 BasicAuthProvider)。
提供商代码非常简单 ATM
public class RoomsAuthProvider : BasicAuthProvider
{
public RoomsAuthProvider(AppSettings appSettings) : base(appSettings)
{
}
public RoomsAuthProvider()
{
}
public override bool TryAuthenticate(IServiceBase authService,
string userName, string password)
{
return true;
}
public override IHttpResult OnAuthenticated(IServiceBase authService,
IAuthSession session, IAuthTokens tokens,
Dictionary<string, string> authInfo)
{
session.FirstName = "some_firstname_from_db";
return base.OnAuthenticated(authService, session, tokens, authInfo);
}
}
我照常注册:
public override void Configure(Funq.Container container)
{
container.Register<ICacheClient>(new MemoryCacheClient());
Plugins.Add(new AuthFeature(() => new AuthUserSession(),
new IAuthProvider[]
{
new RoomsAuthProvider()
}));
Plugins.Add(new ServerEventsFeature());
}
我的客户是:
var client = new ServerEventsClient("http://localhost:1337/", "home")
正在尝试验证:
var authResponse = client.Authenticate(new Authenticate
{
provider = "RoomsAuthProvider",
UserName = "test@gmail.com",
Password = "p@55w0rd",
RememberMe = true,
});
我总是收到 NotFound 错误,并且错误提示没有为此 AuthProvider 设置任何配置。在提供商中设置名称和领域没有帮助。这种类型的客户端是否有另一个身份验证流程,或者我遗漏了什么?欢迎任何想法
当你继承了一个BasicAuthProvider you also inherit it's provider name which unless you override it, is by default is "basic"(即它不是被继承class的名字)。
但是 BasicAuthProvider
实现了 HTTP Basic Auth,因此您应该通过 HTTP 基本身份验证而不是 Web 服务调用来调用它。
如果您尝试通过 Web 服务调用进行身份验证,您应该从 CredentialsAuthProvider instead which is the authentication method used for authenticating via Username/Password like you're doing in your example. The provider name for CredentialsAuthProvider
is credentials 继承,这样您的客户端调用将是:
var authResponse = client.Authenticate(new Authenticate
{
provider = "credentials",
UserName = "test@gmail.com",
Password = "p@55w0rd",
RememberMe = true,
});
我正在尝试从 ServerEventsClient 访问我的自定义 AuthProvider(继承自 BasicAuthProvider)。 提供商代码非常简单 ATM
public class RoomsAuthProvider : BasicAuthProvider
{
public RoomsAuthProvider(AppSettings appSettings) : base(appSettings)
{
}
public RoomsAuthProvider()
{
}
public override bool TryAuthenticate(IServiceBase authService,
string userName, string password)
{
return true;
}
public override IHttpResult OnAuthenticated(IServiceBase authService,
IAuthSession session, IAuthTokens tokens,
Dictionary<string, string> authInfo)
{
session.FirstName = "some_firstname_from_db";
return base.OnAuthenticated(authService, session, tokens, authInfo);
}
}
我照常注册:
public override void Configure(Funq.Container container)
{
container.Register<ICacheClient>(new MemoryCacheClient());
Plugins.Add(new AuthFeature(() => new AuthUserSession(),
new IAuthProvider[]
{
new RoomsAuthProvider()
}));
Plugins.Add(new ServerEventsFeature());
}
我的客户是:
var client = new ServerEventsClient("http://localhost:1337/", "home")
正在尝试验证:
var authResponse = client.Authenticate(new Authenticate
{
provider = "RoomsAuthProvider",
UserName = "test@gmail.com",
Password = "p@55w0rd",
RememberMe = true,
});
我总是收到 NotFound 错误,并且错误提示没有为此 AuthProvider 设置任何配置。在提供商中设置名称和领域没有帮助。这种类型的客户端是否有另一个身份验证流程,或者我遗漏了什么?欢迎任何想法
当你继承了一个BasicAuthProvider you also inherit it's provider name which unless you override it, is by default is "basic"(即它不是被继承class的名字)。
但是 BasicAuthProvider
实现了 HTTP Basic Auth,因此您应该通过 HTTP 基本身份验证而不是 Web 服务调用来调用它。
如果您尝试通过 Web 服务调用进行身份验证,您应该从 CredentialsAuthProvider instead which is the authentication method used for authenticating via Username/Password like you're doing in your example. The provider name for CredentialsAuthProvider
is credentials 继承,这样您的客户端调用将是:
var authResponse = client.Authenticate(new Authenticate
{
provider = "credentials",
UserName = "test@gmail.com",
Password = "p@55w0rd",
RememberMe = true,
});