首页 > 解决方案 > JSON Web 令牌服务堆栈 API

问题描述

我已通过以下方式(以及其他方式)配置了 JSON Web 令牌。

      Plugins.Add(new AuthFeature(() => new AuthUserSession(),
      new IAuthProvider[] {
            new JwtAuthProvider(appSettings) { AuthKeyBase64 = appSettings.GetString("JWT:AuthKeyBase64")},
            new FacebookAuthProvider(appSettings),
            new TwitterAuthProvider(appSettings),
            new CredentialsAuthProvider(appSettings),
      }));

当我尝试使用 reslet 向用户授权时,/auth 我得到 401 Unauthorized,即使我知道用户名和密码是正确的。

还有什么我必须添加到请求中的吗?还是我必须做的其他一些改变?如果我启用了 BasicAuth,则登录没有问题。

标签: servicestack

解决方案


/auth路由仅检查您是否已通过身份验证,如果您未通过身份验证,它将返回 401 指示,否则将返回 200 响应,其中包含有关您的会话的基本信息。

要进行身份验证,您需要使用Auth Providers之一进行身份验证,例如对于用户名/密码,您需要使用以下方式进行身份验证:

/auth/credentials

通过发送GET /auth/credentials?username=myuser&password=mypass通过 HTTP POST

POST /auth/credentials    
{
    "UserName": "admin",
    "Password": "test",
    "RememberMe": true
}

请注意,在 ServiceStack 的下一个版本中,GET Authenticate 请求(如 /auth/credentials)将默认禁用,因此建议在您的应用程序中通过 HTTP POST 进行身份验证。


推荐阅读