假设您已经创建了一个自定义 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;
}
}