gpt4 book ai didi

asp.net-mvc - 在 ServiceStack 服务上进行身份验证后访问客户端上的 AuthSession

转载 作者:行者123 更新时间:2023-12-05 01:07:13 24 4
gpt4 key购买 nike

我对 session 文档有点困惑,所以假设我已经从客户端发送了身份验证数据并像这样检索 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 来检索数据?

最佳答案

假设您已经创建了一个自定义 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 provides access to the current session information on the server like this:使用它作为模型来创建 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;
}
}

关于asp.net-mvc - 在 ServiceStack 服务上进行身份验证后访问客户端上的 AuthSession,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18886768/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com