【发布时间】:2023-03-20 16:43:01
【问题描述】:
我正在尝试通过关注this tutorial 来设置我的 ServiceStack 服务的身份验证。
我的服务用[Authenticate] 属性装饰。
我的 AppHost 如下所示:
public class TestAppHost : AppHostHttpListenerBase
{
public TestAppHost() : base("TestService", typeof(TestService).Assembly) { }
public static void ConfigureAppHost(IAppHost host, Container container)
{
try
{
// Set JSON web services to return idiomatic JSON camelCase properties.
ServiceStack.Text.JsConfig.EmitCamelCaseNames = true;
// Configure the IOC container
IoC.Configure(container);
// Configure ServiceStack authentication to use our custom authentication providers.
var appSettings = new AppSettings();
host.Plugins.Add(new AuthFeature(() =>
new AuthUserSession(), // use ServiceStack's session class but fill it with our own data using our own auth service provider
new IAuthProvider[] {
new UserCredentialsAuthProvider(appSettings)
}));
}
}
UserCredentialsAuthProvider 是我的自定义凭据提供程序:
public class UserCredentialsAuthProvider : CredentialsAuthProvider
{
public override bool TryAuthenticate(IServiceBase authService, string userName, string password)
{
try
{
// Authenticate the user.
var userRepo = authService.TryResolve<IUserRepository>();
var user = userRepo.Authenticate(userName, password);
// Populate session properties.
var session = authService.GetSession();
session.IsAuthenticated = true;
session.CreatedAt = DateTime.UtcNow;
session.DisplayName = user.FullName;
session.UserAuthName = session.UserName = user.Username;
session.UserAuthId = user.ID.ToString();
}
catch (Exception ex)
{
// ... Log exception ...
return false;
}
return true;
}
}
在我的用户测试中,我在 http://127.0.0.1:8888 上初始化并启动我的 TestAppHost,然后使用 JsonServiceClient 向服务验证自己,如下所示:
var client = new JsonServiceClient("http://127.0.0.1:8888/")
var response = client.Send<AuthResponse>(new Auth
{
provider = UserCredentialsAuthProvider.Name,
UserName = username,
Password = password,
RememberMe = true
});
但得到以下异常:
The remote server returned an error: (400) Bad Request.
at System.Net.HttpWebRequest.GetResponse()
at ServiceStack.ServiceClient.Web.ServiceClientBase.Send[TResponse](Object request)...
ServiceStack.ServiceInterface.Auth.Auth 请求包含正确的用户名和密码,请求被发送到:
http://127.0.0.1:8888/json/syncreply/Auth
我不确定为什么 URL 不是 /json/auth/credentials 或者我可能做错了什么。有什么建议吗?
更新
跟踪堆栈中的事件链我发现了以下内容:
JsonDataContractSerializer.SerializeToStream 正确地将 Auth 请求序列化为 Json。但是,EndpointHandlerBase 传递给JsonDataContractDeserializer 的System.Net.HttpRequestStream 具有正确长度的流,其中填充了空值(零字节)。因此,传递给CredentialsAuthProvider.Authenticate 的请求对象的所有属性都为空。
HTTP 流如何去除其数据?
【问题讨论】:
标签: asp.net-mvc authentication servicestack