【问题标题】:ServiceStack JsonServiceClient (TypeScript): Do not go to the requests after authentication from CORSServiceStack JsonServiceClient (TypeScript):在 CORS 认证后不要去请求
【发布时间】:2017-11-22 17:21:29
【问题描述】:

我按照上面说的配置了服务:

Plugins.Add(new CorsFeature(
      allowCredentials: true,
      allowedMethods: "GET, POST, PUT, DELETE, OPTIONS",
      allowedHeaders: "Content-Type, Allow, Authorization, Origin",
      allowOriginWhitelist: new[]
             {
                    "http://localhost:4200",
                    "http://localhost:63342",
                    "http://localhost:63342",
                    "http://localhost:3000",
                    "http://my.site.com"
             }));

我有 2 个函数 Login() 和 GetContacts()

class AppComponent {
***
      constructor(){
        this.client = new JsonServiceClient("http://my.site.com");
      }

      async Login(){

        var auth = new wbs.Authenticate();
        auth.UserName = this.username;
        auth.Password = this.password;

        var authResponse = await this.client.post(auth);

        console.log(authResponse);
      }

      async GetContacts(){
        try {
          this.contacts = await this.client.post(new wbs.Contacts_Get());
          console.log(this.contacts);
        } catch(e) {
          this.contacts = [];
          console.log("Failed to get:", e.responseStatus);
        }
      }
}

"servicestack-client": "0.0.36",

我依次调用这些函数:

1. Login()
2. ContactsGet()

如果我在 IIS express 上本地运行,但是当我部署到 IIS 服务器时它不工作。登录运行正常,但在 Internet Explorer 和 Safari ContactsGet 失败,它返回状态 401,但在 Chrome 中有效。

请帮助我的错误是什么?谢谢!

IIS 设置

更新

var authFeature = new AuthFeature(
        () => new MyUserSession(),
        new[] { new MyCredentialsAuthProvider()
});

public class MyCredentialsAuthProvider : CredentialsAuthProvider {
        private Userslogic users => Userslogic.Instance;

        public override bool TryAuthenticate(IServiceBase authService, string userName, string password) {

            if (!users.CheckUserNameAndPassword(userName, password))
                return false;

            var session = authService.GetSession(false);

            session.IsAuthenticated = true;

            return true;
        }
    }

AuthRequet:

ContactsGetRequest:

【问题讨论】:

  • 请在验证请求之后直接将您的整个AuthFeature 配置以及验证请求和后续请求的原始 HTTP 请求/响应标头发布到Contacts_Get。此外,您应该只使用 client.post(requestDto) 发布 Request DTO,而不是字符串。您可以使用 postToUrl() 发布到 URL,但它需要匹配请求 DTO 的路由,即类似于 /contacts 的东西,并且由于它是一个 POST,它应该包含一个请求 DTO 的正文。对于Contacts_Get 服务,您可能希望改用client.get(new Contacts_Get())
  • @mythz 我更新了问题。你说得对,我不是代码的忠实版本:this.contacts = await this.client.post(new wbs.Contacts_Get());

标签: cors servicestack


【解决方案1】:

您的问题是因为客户端没有在请求中发送 Cookie,并且由于您的 CORS 配置无效,因此没有发送它们,allowOriginWhitelist URL 需要与请求的主机名完全匹配发送到,在这种情况下是http://109.120.152.165

只要 HTTP 客户端中的后续请求不遵守上一个请求的 Set-Cookie 响应标头,您的 CORS 配置就无效,您需要调查原因。

【讨论】:

  • 感谢您的反馈!
【解决方案2】:

在问题研究中发现问题出在客户端而不是服务端ServiceStack,这样的限制有浏览器IE和Safari。

  1. IE - https://stackoverflow.com/a/22450633/7015077
  2. Safari - https://stackoverflow.com/a/486569/7015077

【讨论】:

    猜你喜欢
    • 2023-03-20
    • 1970-01-01
    • 2021-12-03
    • 2015-03-10
    • 2016-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-22
    相关资源
    最近更新 更多