gpt4 book ai didi

web-services - 具有基本身份验证和 SetCredentials 的 ServiceStack Web 服务

转载 作者:行者123 更新时间:2023-12-04 04:14:35 25 4
gpt4 key购买 nike

现在我正在玩 ServiceStack和它的 Authentication and authorization支持。

处理 CustomAuthenticationMvc (Web 服务 Host 应用程序)来自 Use Cases project在尝试了解身份验证的工作原理时,我这样做了:

public class AppHost : AppHostBase
{
public AppHost() : base("Basic Authentication Example",
typeof(AppHost).Assembly) { }

public override void Configure(Container container)
{
// register storage for user sessions
container.Register<ICacheClient>(new MemoryCacheClient());
container.Register<ISessionFactory>(c => new SessionFactory(
c.Resolve<ICacheClient>()));

//Register AuthFeature with custom user session and Basic auth provider
Plugins.Add(new AuthFeature(
() => new CustomUserSession(),
new AuthProvider[] { new BasicAuthProvider() }
) );

var userRep = new InMemoryAuthRepository();
container.Register<IUserAuthRepository>(userRep);

//Add a user for testing purposes
string hash;
string salt;
new SaltedHash().GetHashAndSaltString("test", out hash, out salt);
userRep.CreateUserAuth(new UserAuth
{
Id = 1,
DisplayName = "DisplayName",
Email = "as@if.com",
UserName = "john",
FirstName = "FirstName",
LastName = "LastName",
PasswordHash = hash,
Salt = salt,
}, "test");
}
}

服务实现是著名的 Hello World 服务,带有 [Authenticate]应用在请求 DTO 正上方的属性...
[Authenticate]
[Route("/hello")]
[Route("/hello/{Name}")]
public class HelloRequest : IReturn<HelloResponse>
{
public string Name { get; set; }
}

public class HelloResponse
{
public string Result { get; set; }
}

public class HelloService : IService<HelloRequest>
{
public object Execute(HelloRequest request)
{
return new HelloResponse
{
Result = "Hello, " + request.Name
};
}
}

将控制台应用程序用作 Client ,我有这个代码:
static void Main(string[] args)
{
// The host ASP.NET MVC is working OK! When I visit this URL I see the
// metadata page.
const string BaseUrl = "http://localhost:8088/api";

var restClient = new JsonServiceClient(BaseUrl);

restClient.SetCredentials("john", "test");

HelloResponse response = restClient.Get<HelloResponse>("/hello/Leniel");

Console.WriteLine(response.Result);
}

这里的问题是我不认为 restClient.SetCredentials("john", "test");正在按我的预期工作,因为网络服务调用 restClient.Get<HelloResponse>("/hello/Leniel");正在抛出异常。我看到我被重定向到登录页面...我认为设置凭据可以调用 Web 服务。这里不是这样。

我究竟做错了什么?我计划从 MonoTouch iOS 应用程序调用此 Web 服务,用户将在该应用程序中在一个简单的对话框窗口中输入他的用户名和密码,但在此之前,我希望在这个简单的控制台应用程序中运行它。

编辑 1

我尝试使用此代码发送身份验证请求(如 thread 所示):
var authResponse = restClient.Send<AuthResponse>(new Auth
{
UserName = "john",
Password = "test",
RememberMe = true,
});

但抛出异常:

Type definitions should start with a '{', expecting serialized type 'AuthResponse', got string starting with:

<head>


看起来我有一个配置错误的服务,因为它不断将我重定向到默认的 ASP.NET MVC Login页面(请参阅下面的 Fiddler 屏幕截图)。我也换了 AuthFeature注册通过 nullHtmlRedirect .
//Register AuthFeature with custom user session and Basic auth provider
Plugins.Add(new AuthFeature(
() => new CustomUserSession(),
new AuthProvider[] { new BasicAuthProvider() }
) { HtmlRedirect = null });

编辑 2

一起玩 Fiddler我尝试添加授权请求 header ,如 here 所述:

enter image description here

查看 Fiddler 中的 Auth 检查器,我可以看到:
Authorization Header is present: Basic am9objp0ZXN0
Decoded Username:Password= john:test

现在,为什么我会得到 401 未经授权的状态?鉴于我在内存中设置了一个用户用于测试目的,它不应该工作吗?

如何在我的示例控制台应用程序中传递请求 header ?

如果我在控制台应用程序中这样做:
restClient.AlwaysSendBasicAuthHeader = true;

我收到 401 未经授权的回复...

编辑 3

在 desunit 的回答和代码添加的帮助下,我设法使控制台应用程序身份验证使用以下代码:
class Program
{
const string BaseUrl = "http://localhost:8088/api";

static void Main(string[] args)
{
var restClient = new JsonServiceClient(BaseUrl);

restClient.SetCredentials("john", "test");

restClient.AlwaysSendBasicAuthHeader = true;

HelloResponse response = restClient.Get<HelloResponse>("/hello/Leniel");

Console.WriteLine(response.Result);
}
}

//Response DTO
//Follows naming convention
public class HelloResponse
{
public string Result { get; set; }
}

我现在想知道以下内容:在控制台应用程序中,我应该始终发送基本身份验证 header 吗?如果我删除该行,身份验证就会失败并且我没有收到 HelloResponse.

最佳答案

问题与 ServiceStack 本身无关,而是与 ASP.NET web 处理 401 错误代码的方式有关。您可以使用自定义 IHttpModule 来解决它禁止重定向到登录表单。有关该问题的更多详细信息可以阅读 here .

因为这是很常见的问题 we have updated CustomAuthenticationMvc项目来演示可能的解决方案。

关于web-services - 具有基本身份验证和 SetCredentials 的 ServiceStack Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13573793/

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