Adding basic HTTP auth to a WCF REST service

前端 未结 3 1493
太阳男子
太阳男子 2020-12-02 13:43

I have a WCF HTTP REST Service and I tie into it with an HTTP client in a different programming language who writes its own custom HTTP. I would like to add WWW-Authenticate

3条回答
  •  [愿得一人]
    2020-12-02 14:38

    Just to add to this, Chrome will not load the login dialog unless you change "BasicRealm" to "BasicRealm=site" in the OnEndRequest method:

        public void OnEndRequest(object source, EventArgs eventArgs)
        {
            if (HttpContext.Current.Response.StatusCode == 401)
            {
                //if the status is 401 the WWW-Authenticated is added to 
                //the response so client knows it needs to send credentials 
                HttpContext context = HttpContext.Current;
                context.Response.StatusCode = 401;
                context.Response.AddHeader("WWW-Authenticate", "Basic Realm=site");
            }
        }
    

    And thanks, this is such a simple solution.

提交回复
热议问题