【问题标题】:Using CustomCredentialsAuthProvider in JsonServiceClient在 JsonServiceClient 中使用 CustomCredentialsAuthProvider
【发布时间】: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


    【解决方案1】:

    CredentialsAuthProvider.Name 只是提供对"credentials" 字符串文字的类型化访问,您可以使用它来代替它,例如:

    var authResponse = client.Post(new Authenticate
    {
        provider = "credentials",
        UserName = "testuser",
        Password = "1234",
        RememberMe = true,
    });
    

    您可以在身份验证文档中找到Auth provider literals 的列表。

    【讨论】:

      猜你喜欢
      • 2012-10-29
      • 2013-10-14
      • 1970-01-01
      • 2021-12-03
      • 1970-01-01
      • 2022-06-28
      • 2016-12-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多