【发布时间】:2016-10-06 20:01:15
【问题描述】:
我尝试实现我自己的自定义 CredentialsAuthProvider。服务器似乎可以在以下实现中正常工作:
public class MyCustomCredentialsAuthProvider : CredentialsAuthProvider
{
public override bool TryAuthenticate(IServiceBase authService, string userName, string password)
{
if (userName == "testuser" && password == "1234")
{
return true;
}
else
{
return false;
}
}
public override IHttpResult OnAuthenticated(IServiceBase authService, IAuthSession session, IAuthTokens tokens,
Dictionary<string, string> authInfo)
{
session.FirstName = "Testuser Joe Doe";
authService.SaveSession(session, SessionExpiry);
return null;
}
}
当我调用浏览器http://localhost:8088/auth/credentials?UserName=testuser&Password=1234 时,我会返回一个包含会话ID 和测试用户Joe Doe 的页面。看起来不错。
现在我尝试从我的 Windows WPF 客户端调用它。自从我实现了 MVVM 模式以来,我已经创建了一个 Login Page 和一个 LoginViewModel 类。但我不明白,我真正必须将 Authenticate 类中的 provider 属性设置为什么。
在我的 WPF 类中,我有以下内容:
public partial class App : Application
{
public JsonServiceClient ServiceClient { get; private set; }
public App()
{
this.InitializeComponent();
}
// ....
}
然后在我的 LoginViewModel 中,我有一个 Login() 方法,它是登录按钮的 RelayCommand 实现,就像这样(表单还包含一个字段,您必须在其中输入应用程序服务器的名称,因为有多个. 这就是我在处理程序中编写 baseUri 的原因):
private void Login()
{
var baseUri = $"http://{AppServer}:8088";
((App)Application.Current).InitServiceClient(baseUri);
var client = ((App) Application.Current).ServiceClient;
//var response = client.Send<AuthResponse>(new Auth { UserName = "Test", Password = "TestPassword" });
var authResponse = client.Post(new Authenticate
{
provider = CredentialsAuthProvider.Name, // <-- WHAT SHOULD THIS BE???
UserName = "testuser",
Password = "1234",
RememberMe = true,
});
// ....
}
CredentialsAuthProvider 编译器未知。我需要在这里传递什么以及我需要什么程序集?到目前为止,我有:
- ServiceStack.Ckient
- ServiceStack.Interfaces
- ServiceStack.Text
- MyService.ServiceModel //DLL 包含 DTO 等,没有实现
我在这里错过了什么和做错了什么?
【问题讨论】:
标签: servicestack