Servicestack如何让jsonserviceclient使用自定义身份验证

时间:2013-02-01 15:41:45

标签: servicestack

查看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服务。我使用bog标准webforms进行编程,因此MVC示例对我来说效果不好。我只是在为我的特定场景寻找最佳解决方案,我可以使用.NET中的webforms对大多数客户验证我的webservice的调用者

1 个答案:

答案 0 :(得分: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
    }));