I Have a service website named A, and a winform client named B. B called A servicestack service and need Authenticate. in the A has AppHost.cs
public partial class AppHost : AppHostBase
{
public AppHost()
: base(g.ApplicationName, typeof(AppHost).Assembly)
{
typeof(Authenticate).AddAttributes(new ExcludeAttribute(Feature.Metadata));
}
public override void Configure(Container container)
{
//Set MVC to use the same Funq IOC as ServiceStack
ControllerBuilder.Current.SetControllerFactory(
new FunqControllerFactory(container));
SetConfig(new HostConfig
{
DebugMode = g.IsDemo, // Show StackTrace in Web Service Exceptions
EnableFeatures = g.IsDemo ? Feature.All : Feature.All.Remove(Feature.Metadata),
HandlerFactoryPath = "api",
GlobalResponseHeaders = { //Support CORS
{ "Access-Control-Allow-Origin", "*" },
{ "Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS" },
{ "Access-Control-Allow-Headers", "Content-Type" },},
DefaultContentType = MimeTypes.Json // Default Return Json
});
//1. Registering the authorization provider.
Plugins.Add(new AuthFeature(() => new CustomUserSession(),
new IAuthProvider[]
{
new BasicAuthProvider(), //Sign-in with Basic Auth
new CredentialsAuthProvider(),
})
{
IncludeRegistrationService = false,
MaxLoginAttempts = 5,
IsValidUsernameFn = delegate (string name)
{
if (name.IsNullOrEmpty()) return false;
return name.Length >= 1 && name.Length <= 32;
},
});
}
In the winform client B, In the AppHost, If I used the below code and in othe place used the Gateway , works fine.
public partial class AppHost : AppHostBase
{
public AppHost()
: base(g.ApplicationName, typeof(AppHost).Assembly)
{
}
public override void Configure(Container container)
{
container.Register<IServiceGateway>(GetNewClient());
}
public JsonServiceClient GetNewClient()
{
var _client = new JsonServiceClient(f.apiurl_weixin)
{
UserName = "username",
Password = "password"
};
return _client;
}
}
if I replace the JsonServiceClient to JsonHttpClient like below
public partial class AppHost : AppHostBase
{
public AppHost()
: base(g.ApplicationName, typeof(AppHost).Assembly)
{
}
public override void Configure(Container container)
{
container.Register<IServiceGateway>(GetNewClient());
}
public JsonHttpClient GetNewClient()
{
var _client = new JsonHttpClient(f.apiurl_weixin)
{
UserName = "username",
Password = "password"
};
return _client;
}
}
when I used the Gateway.Send method to invoke the service from the site A. It will get the error
============================================
2016-05-31 19:42:33.0885 ăERRORăăweixin****ă ****.ModifyData Unauthorized ServiceStack.WebServiceException WebServiceException 401 Unauthorized Code: Unauthorized, Message: T GetSyncResponse[T](System.Threading.Tasks.Task`1[T]) ĺ¨ ServiceStack.InternalExtensions.GetSyncResponse[T](Task`1 task)
in ServiceStack.JsonHttpClient.Send[TResponse](Object request)
in weixin.****.ModifyData() location C:\****.cs:line 104
============================================