【问题标题】:ServiceStack UserName should not be empty?ServiceStack 用户名不应该为空?
【发布时间】:2014-09-24 23:09:47
【问题描述】:

我正在根据文档发送以下 POST 请求:

POST http://localhost:56049/auth/credentials?format=json HTTP/1.1
User-Agent: Fiddler
Host: localhost:56049
Content-Length: 64

{
    "UserName" : "admin",
    "Password" : "test",
    "RememberMe" : true
}

然而,它给了我一个意想不到的结果:

{"responseStatus":{"errorCode":"ValidationException","message":"Validation failed: 
\r\n -- 'User Name' should not be empty.\r\n -- 'Password' should not be empty.",
"stackTrace":[...]

为什么它认为我的用户名和密码为空?

参考。 https://github.com/ServiceStack/ServiceStack/wiki/Authentication-and-authorization

【问题讨论】:

    标签: authentication servicestack


    【解决方案1】:

    Request的Content-Type应该在HTTP Headers中,试试:

    POST http://localhost:56049/auth/credentials HTTP/1.1
    Host: localhost:560490
    User-Agent: Fiddler
    Content-Type: application/json
    Content-Length: 64
    
    {
        "UserName" : "admin",
        "Password" : "test",
        "RememberMe" : true
    }
    

    【讨论】:

    • 你说的太对了!谢谢,我忘了那一点。我的错。
    • 这是在 Fiddler 中吗?如果是这样,您需要添加在每个 HTTP 请求的 HTTP 响应标头中返回的 ss-id/ss-pid cookie。
    • 好的,找到答案了。需要像这样添加一个组合标题:Cookie: ss-id=2hjtligETL7tZQNyzLJz; path=/; HttpOnly;ss-pid=fRDsiV7CQPmK6gnevvCP; expires=Sun, 24-Sep-2034 16:03:25 GMT; path=/; HttpOnly;ss-opt=perm; expires=Sun, 24-Sep-2034 16:03:25 GMT; path=/; HttpOnly
    • @user1477388 是的,这就是 cookie 格式。在 ServiceStack 的下一个 v4.0.32+ 中,您将使用 be able to use explicit HTTP Request HeadersX-ss-optX-ss-idX-ss-pid 而不是 Cookie。
    • @user1477388 因为它返回一个自定义响应,您需要创建一个自定义 AuthProvider 并覆盖 Authenticate() API 以返回所需的响应。要对后续请求进行身份验证,您将实现 IAuthWithRequest.PreAuthenticate() 方法,请参阅 BasicAuthProvider.PreAuthenticate() 示例。没有时间构建这个自动取款机,但您可以submit a feature request,以便我们衡量对它的兴趣。
    猜你喜欢
    • 2013-03-20
    • 1970-01-01
    • 2021-05-30
    • 2021-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多