4

我对会话文档有点困惑,所以假设我已经从客户端发送了身份验证数据并像这样检索 ss-id 和 ss-pid:

var client = new JsonServiceClient("http://somewhere/theAPI/");
var response = client.Post(new Auth() {UserName = "myuser", Password = "password123"});
var myCookie= client.CookieContainer.GetCookies(new Uri("http://somewhere/theAPI"));

如何从服务堆栈中检索 AuthSession 信息,例如姓氏、电子邮件等?我是否需要将它存储在内存缓存服务器等其他地方,然后从中检索?

或者我需要在客户端建立我的身份验证?并且只使用 API 来检索数据?

4

1 回答 1

5

假设您已经创建了一个自定义 AuthUserSession,例如:

/// <summary>
/// Create your own strong-typed Custom AuthUserSession where you can add additional AuthUserSession 
/// fields required for your application. The base class is automatically populated with 
/// User Data as and when they authenticate with your application. 
/// </summary>
public class CustomUserSession : AuthUserSession {
    public string CustomId { get; set; }
}

并且您在配置 AuthFeature 插件时注册了您的自定义 AuthUserSession,如下所示:

public override void Configure(Container container)
{
    //Register all Authentication methods you want to enable for this web app.            
    Plugins.Add(new AuthFeature(
        () => new CustomUserSession(), //Use your own typed Custom UserSession type
        new IAuthProvider[] {
            new CredentialsAuthProvider(),              //HTML Form post of UserName/Password credentials
            // and any other auth providers you need
        }));
}

然后,您可以在您创建的服务中将此数据公开给客户端。 SocialBotstrapApi 提供对服务器上当前会话信息的访问,如下所示: 使用它作为模型来创建 UserAuth 服务,该服务仅返回当前用户的信息。

public abstract class AppServiceBase : Service {
    private CustomUserSession userSession;
    protected CustomUserSession UserSession {
        get {
            return base.SessionAs<CustomUserSession>();
        }
    }
}


[Route("/userauths")]
public class UserAuths
{
    public int[] Ids { get; set; }
}

public class UserAuthsResponse
{
    public UserAuthsResponse()
    {
        this.Users = new List<User>();
        this.UserAuths = new List<UserAuth>();
        this.OAuthProviders = new List<UserOAuthProvider>();
    }
    public CustomUserSession UserSession { get; set; }

    public List<User> Users { get; set; }

    public List<UserAuth> UserAuths { get; set; }

    public List<UserOAuthProvider> OAuthProviders { get; set; }
}

//Implementation. Can be called via any endpoint or format, see: http://servicestack.net/ServiceStack.Hello/
public class UserAuthsService : AppServiceBase
{
    public object Any(UserAuths request)
    {
        var response = new UserAuthsResponse {
            UserSession = base.UserSession,
            Users = Db.Select<User>(),
            UserAuths = Db.Select<UserAuth>(),
            OAuthProviders = Db.Select<UserOAuthProvider>(),
        };

        response.UserAuths.ForEach(x => x.PasswordHash = "[Redacted]");
        response.OAuthProviders.ForEach(x =>
            x.AccessToken = x.AccessTokenSecret = x.RequestTokenSecret = "[Redacted]");
        if (response.UserSession != null)
            response.UserSession.ProviderOAuthAccess.ForEach(x =>
            x.AccessToken = x.AccessTokenSecret = x.RequestTokenSecret = "[Redacted]");

        return response;
    }
}
于 2013-09-19T16:48:57.457 回答