【问题标题】:Servicestack How to get the jsonserviceclient to work with custom authenticationServicestack 如何让 jsonserviceclient 使用自定义身份验证
【发布时间】:2013-02-01 15:41:45
【问题描述】:

查看 ServiceStack.UseCases 示例项目。在调用身份验证服务后,我正在尝试使用 jsonserviceclient 调用 HelloRequest 服务。无论我做什么,它似乎都失败并返回 Not Found 错误消息。有谁知道我做错了什么?

   protected void Button1_Click(object sender, EventArgs e)
    {
        var baseUrl = Request.Url.GetLeftPart(UriPartial.Authority) + "/api";
        var client = new JsonServiceClient(baseUrl);
        client.UserName = "admin";
        client.Password = "123";
        client.SetCredentials("admin", "123");
        client.AlwaysSendBasicAuthHeader = true;
        client.Send(new HelloRequest { Name = "Mike" });
    }

服务器的服务配置如下

 public class AppHost : AppHostBase
{
    public AppHost() : base("Custom Authentication Example", typeof(AppHost).Assembly) { }

    public override void Configure(Container container)
    {
        // register storage for user sessions 
        container.Register<ICacheClient>(new MemoryCacheClient());

        // Register AuthFeature with custom user session and custom auth provider
        Plugins.Add(new AuthFeature(
            () => new CustomUserSession(), 
            new[] { new CustomCredentialsAuthProvider() }
        ));
    }
}

我真正想要的是一个很好的解决方案来解决我遇到的以下问题。我有一个具有现有用户数据库和自定义身份验证过程的现有系统。我现在正在尝试使用 servicestack 将系统的功能公开为 Web 服务。我使用沼泽标准网络表单进行编程,因此 MVC 示例对我来说效果不佳。我只是在寻找适合我的特定场景的最佳解决方案,我可以使用 .NET 中的大多数客户对我的 web 服务的调用者进行身份验证

【问题讨论】:

    标签: servicestack


    【解决方案1】:

    您还需要在您尝试进行身份验证的服务器上启用基本身份验证。 示例见SocialBootstrapApi AppHost

    Plugins.Add(new AuthFeature(
        () => new CustomUserSession(), //Use your own typed Custom UserSession type
        new IAuthProvider[] {
            new BasicAuthProvider(),                    //Sign-in with Basic Auth
        }));
    

    【讨论】:

    • 这个例子是使用 CustomCredentialsAuthProvider,它是一个扩展 CredentialsAuthProvider 的类。你是说我需要摆脱它并改用 BasicAuthProvider 吗?
    • 是的,它需要从BasicAuthProvider继承。
    • 现在似乎完全崩溃了。收到错误消息内部服务器错误。
    • 这是针对任何错误的通用 ASP.NET 错误消息,您需要找出底层异常是什么并从那里解决。
    • 嗨 Mythz,您对我的特定场景有什么建议吗?我有一个现有的网络应用程序。我打算使用 servicestack 来公开我的数据库中的功能。我想针对我现有的数据库对服务调用者进行身份验证。客户端的主要选择是使用 jsonserviceclient 的 webforms。
    猜你喜欢
    • 2013-11-02
    • 1970-01-01
    • 1970-01-01
    • 2013-11-15
    • 2018-03-02
    • 2020-04-01
    • 2022-12-10
    • 2011-09-01
    • 2012-06-22
    相关资源
    最近更新 更多