wcf 401并在RESTful服务中请求登录/密码

时间:2011-06-05 21:20:53

标签: c# wcf http rest

我正在使用基本授权编写RESTful服务。

以下是没有授权标题或UN /密码错误

时的操作
//Get authorization header
            var auth = HttpContext.Current.Request.Headers.GetValues("Authorization");
            if (auth == null)
            {
                outgoingResponse.StatusCode = HttpStatusCode.Unauthorized;
                return false;
            }

            //Parse auth header:
            var authString = auth[0];
            String loginName, password;
            try
            {
                var decbuff = Convert.FromBase64String(authString.Replace("Basic ", ""));
                loginName = System.Text.Encoding.UTF8.GetString(decbuff).Split(':')[0];
                password = System.Text.Encoding.UTF8.GetString(decbuff).Split(':')[1];
            }
            catch
            {
                outgoingResponse.StatusCode = HttpStatusCode.Unauthorized;
                outgoingResponse.StatusDescription = "Invalid Authorization header";
                return false;
            }

当我看到提琴手时,我看到了这一点:

enter image description here

它适用于我的客户端(Android),但我希望通过资源管理器或其他浏览器可以浏览此服务。如果我发送401,如何让资源管理器询问UN /密码?我需要指定一些内容吗?

由于

1 个答案:

答案 0 :(得分:0)

我明白了。我需要像这样将HTTP标头和401一起放入:

   WWW-Authenticate:Basic realm =“Secure   区域“

然后浏览器知道并向用户显示登录窗口

相关问题