C# Servicestack用户会话不工作
我在ServiceStack中编写了一个API,我正在尝试为客户端构建身份验证。目前,这个API只能由Android客户端(Xamarin/C#)访问。API本身运行在带有Apache/mod_mono的Debian服务器上 在阅读了Github之后,我仍然无法100%确定如何以这样一种方式将其组合在一起。。。一旦客户端提供了有效的凭据(用于测试、基本HTTP身份验证),用户将获得一个会话,并且在来自同一会话的后续请求中不会再次检查凭据 AppHost类:C# Servicestack用户会话不工作,c#,session,authentication,
servicestack,mod-mono,C#,Session,Authentication,
servicestack,Mod Mono,我在ServiceStack中编写了一个API,我正在尝试为客户端构建身份验证。目前,这个API只能由Android客户端(Xamarin/C#)访问。API本身运行在带有Apache/mod_mono的Debian服务器上 在阅读了Github之后,我仍然无法100%确定如何以这样一种方式将其组合在一起。。。一旦客户端提供了有效的凭据(用于测试、基本HTTP身份验证),用户将获得一个会话,并且在来自同一会话的后续请求中不会再次检查凭据 AppHost类: { public class AppH
{
public class AppHost
: AppHostBase
{
public AppHost() //Tell ServiceStack the name and where to find your web services
: base("Service Call Service", typeof(ServiceCallsService).Assembly) { }
public override void Configure(Funq.Container container)
{
//Set JSON web services to return idiomatic JSON camelCase properties
ServiceStack.Text.JsConfig.EmitCamelCaseNames = true;
// Session storage
container.Register<ICacheClient>(new MemoryCacheClient());
// auth feature and session feature
Plugins.Add(new AuthFeature(
() => new AuthUserSession(),
new[] { new userAuth() }
) { HtmlRedirect = null } );
}
public class userAuth : BasicAuthProvider
{
public override bool TryAuthenticate(IServiceBase authService, string userName, string password)
{
peruseAuth peruseAuthHandler= new peruseAuth();
errorLogging MyErrorHandler = new errorLogging() ;
if (peruseAuthHandler.ValidateUser(authService, userName, password))
{
try
{
var session = (AuthUserSession)authService.GetSession(false);
session.UserAuthId = userName;
session.IsAuthenticated = true;
return true;
}
catch(Exception ex)
{
MyErrorHandler.LogError(ex, this) ;
return false ;
}
}
else
{
Console.Write("False");
return false;
}
}
}
我已经做了一些日志记录,似乎每次客户端执行操作时,都会根据数据库检查其用户名/密码。这里有什么不对劲吗,还是这是预期的结果 对于在每个请求上发送凭据的位置,它是预期的
为了让ServiceClient保留已通过身份验证的会话cookie,您应该在进行身份验证时设置RememberMe
标志,例如使用:
这会将用户会话附加到ss-pid
cookie(永久会话cookie),客户端会保留该cookie,并在来自该ServiceClient实例的后续请求中重新发送该cookie。对于在每个请求中发送凭据的位置,它是预期的
为了让ServiceClient保留已通过身份验证的会话cookie,您应该在进行身份验证时设置RememberMe
标志,例如使用:
这会将用户会话附加到ss-pid
cookie(永久会话cookie)上,客户端将保留该cookie并在来自该ServiceClient实例的后续请求中重新发送
btnLogin.Click += (sender, e) =>
{
// Set credentials from EditText elements in Main.axml
client.SetCredentials(txtUser.Text, txtPass.Text);
// Force the JsonServiceClient to always use the auth header
client.AlwaysSendBasicAuthHeader = true;
};
var client = new JsonServiceClient(BaseUrl);
var authResponse = client.Send(new Authenticate {
provider = "credentials",
UserName = "user",
Password = "p@55word",
RememberMe = true,
});