不工作Servicestack用户会话

不工作Servicestack用户会话

本文介绍了不工作Servicestack用户会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写在ServiceStack一个API,我试图在认证建立为客户。目前,该API将只搭载Android客户端(Xamarin / C#)来访问。本身是与Apache /是mod_mono Debian服务器

I have an API written in ServiceStack and I am attempting to build in authentication for clients. At the moment this API will only be accessed by Android clients (Xamarin / C#). The API itself is running on a Debian server with Apache / mod_mono

在Github上看完后,我仍然不是100%确定如何以这样的方式把这个在一起......一旦客户端已提供有效的凭据(用于测试,基本的HTTP认证)用户得到一个会话,凭据不会从同一会话的后续请求再次检查。

After reading up on Github, I am still not 100% sure how to put this together in such a way that... once the client has provided valid credentials (For testing, basic HTTP authentication) the user gets a session and the credentials are not checked again on subsequent requests from the same session.

APPHOST类:

{
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;
            }
        }
    }

的JsonServiceClient: (就在登录事件)

        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;

            };

我已经做了一些记录,并且似乎每一次客户端执行他们的用户名/密码,正在对数据库检查行动。是不是有什么错在这里,或这是预期的结果?

I've been doing a bit of logging, and it seems that every time the client performs an action their username/password is being checked against the database. Is there something wrong here, or is this the expected result?

推荐答案

有关哪里凭据是在它的预期每个请求发送。

For Basic Auth where the credentials are sent on each request it's expected.

为了为ServiceClient保留已经验证,你应该设置与rememberMe 标志时您进行验证,例如使用 CredentialsAuthProvider :

In order for the ServiceClient to retain the Session cookies that have been authenticated you should set the RememberMe flag when you authenticate, e.g using CredentialsAuthProvider:

var client = new JsonServiceClient(BaseUrl);
var authResponse = client.Send(new Authenticate {
    provider = "credentials",
    UserName = "user",
    Password = "p@55word",
    RememberMe = true,
});

这背后附加的用户会话的 SS-PID 饼干(永久会话Cookie),其中客户端保留并重新发送对从ServiceClient实例。

Behind the scenes this attaches the user session to the ss-pid cookie (permanent session cookie), which the client retains and resends on subsequent requests from that ServiceClient instance.

这篇关于不工作Servicestack用户会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 16:10