【发布时间】: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:
【问题讨论】:
-
请在验证请求之后直接将您的整个
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