我手动实施的基本 HTTP 身份验证有什么问题吗?
我正在使用 WCF 开发一个 RESTful Web 服务,该服务充当一组存储过程的代理,出于安全原因,面向 Internet 的应用程序无法直接访问这些存储过程。
实现服务的 DataContract 的类有一个辅助属性,用于检索当前登录用户的名称,或者如果当前没有用户登录,则生成 HTTP 401 未经授权的响应。
[ServiceContract]
public class MyService
{
// The helper property
string UserName
{
get
{
WebOperationContext context = WebOperationContext.Current;
if (context != null)
{
string authHeader = context.IncomingRequest.Headers[HttpRequestHeader.Authorization];
if (authHeader != null && authHeader.StartsWith("Basic "))
{
string string64 = authHeader.Substring(6);
byte[] array64 = Convert.FromBase64String(string64);
string decoded = ASCIIEncoding.ASCII.GetString(array64);
string[] authParts = decoded.Split(':');
if (ValidateLogin(authParts[0] /*userName*/,
authParts[1] /*password*/))
return authParts[0];
}
}
OutgoingWebResponseContext outgoing = context.OutgoingResponse;
outgoing.StatusCode = HttpStatusCode.Unauthorized;
outgoing.Headers[HttpResponseHeader.WwwAuthenticate] = "Basic";
return null;
}
}
[OperationContract]
public int LengthOfUserName()
{
return UserName.Length;
}
}
但是,当我尝试使用有效的用户名和密码登录,我仍然收到未经授权的错误。我的代码有什么问题吗?
I am using WCF to develop a RESTful Web Service that acts as a proxy to a set of stored procedures that, for security reasons, cannot be directly accessed by Internet-facing applications.
The class implementing the service's DataContract
has a helper property that retrieves the name of the currently logged-in user, or generates an HTTP 401 Unauthorized response if no user is currently logged in.
[ServiceContract]
public class MyService
{
// The helper property
string UserName
{
get
{
WebOperationContext context = WebOperationContext.Current;
if (context != null)
{
string authHeader = context.IncomingRequest.Headers[HttpRequestHeader.Authorization];
if (authHeader != null && authHeader.StartsWith("Basic "))
{
string string64 = authHeader.Substring(6);
byte[] array64 = Convert.FromBase64String(string64);
string decoded = ASCIIEncoding.ASCII.GetString(array64);
string[] authParts = decoded.Split(':');
if (ValidateLogin(authParts[0] /*userName*/,
authParts[1] /*password*/))
return authParts[0];
}
}
OutgoingWebResponseContext outgoing = context.OutgoingResponse;
outgoing.StatusCode = HttpStatusCode.Unauthorized;
outgoing.Headers[HttpResponseHeader.WwwAuthenticate] = "Basic";
return null;
}
}
[OperationContract]
public int LengthOfUserName()
{
return UserName.Length;
}
}
However, when I attempt to log in with a valid user name and password, I still get an Unauthorized error. What's wrong with my code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论