摘要式身份验证返回未经授权

问题描述

我正在尝试通过摘要式身份验证进行身份验证,并且API返回未授权,并且当我检查cookie的数量时,我可以看到cookie没有传递给实际的请求。但是,我首先通过获取cookie并添加并提出请求,使它通过邮递员工作。但是,当我通过代码执行此操作时,我将遭到未经授权的访问。

请指导我如何使用摘要身份验证类型进行身份验证。

方法获取Cookie

     private RestClient _Client = new RestClient();
     public API()
     {
        _Client = new RestClient(_BaseUrl);
        _Client.Authenticator = _Creds;
        if (_SessionEnabled)
        {
            var request = new RestRequest("admin/getsession",Method.GET);

            IRestResponse response = _Client.Execute(request);
            var content = response.Content;

            _Cookie = response.Cookies;
            SCHelper.CheckLinOTPResponse(response);
            
        }
        

将会话添加到实际请求中

   public static RestRequest AddSessionToRequest(RestRequest Request,IList<RestResponseCookie> Cookie,bool SessionEnabled)
    {
        if(SessionEnabled)
        {
            Request.AddQueryParameter("session",Cookie[0].Value);
            Request.AddParameter(Cookie[0].Name,Cookie[0].Value,ParameterType.Cookie);
        }            
        return Request;
    }

这是通过首先使用摘要式身份验证机制进行身份验证来生成OTP的方法。它在此处失败并显示未经授权。

   public string GenerateOtp(string Serial,string User,string Realm)
    {
        try
        {
            string username = "";
            string password = "";

            var client = _Client;
            var credentials = username + ":" + password;
            var base64Credentials = Convert.ToBase64String(Encoding.UTF8.GetBytes(credentials));
            var request = new RestRequest("gettoken/getotp",Method.GET);
            //client.Authenticator = new HttpBasicAuthenticator(username,password); //tried to add this and it didn't work
            request.AddHeader("Authorization","Digest " + base64Credentials);

            //check session
            request = SCHelper.AddSessionToRequest(request,_Cookie,_SessionEnabled);
            request.AddParameter("user",User,ParameterType.QueryString);
            request.AddParameter("realm","myRealm",ParameterType.QueryString);
            request.AddParameter("serial",Serial,ParameterType.QueryString);

            IRestResponse response = client.Execute(request);  //This is were it fails.
            SCHelper.CheckLinOTPResponse(response);
            var content = response.Content;
            return content;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

解决方法

我终于想出了一些解决方案。我必须添加标头并从实际的RestRequest调用中获取Cookie,并且连接成功。


library(dplyr)
library(fastDummies)

teams <- results$Home %>% unique()

# function to add a dummy for a given team is either Home or Away 
add_HoA <- function(df,team) {
  HoA_str <- paste0('HoA_',team)
  HoA <- ensym(HoA_str)
  
  df <- df %>% mutate(!!HoA := (Home ==team | Away==team) %>% as.integer())
  return (df)
}

for (team in teams) {
  results <- add_HoA(results,team)
}

# using HoA_ variables for all teams  
model2 <- glm(HomeWin ~ .,family = binomial(link="probit"),data = results %>% dplyr::select(HomeWin,starts_with('HoA_')))
summary(model2)

results <- fastDummies::dummy_cols(results,select_columns = c('Home','Away'))

# using HoA_ variables for NY
model3 <- glm(HomeWin ~ .,data = results %>%
                dplyr::select(HomeWin,HoA_NY,starts_with('Home_'),starts_with('Away_')) %>%
                dplyr::select(-Home_NY,-Away_NY))
summary(model3)

# using HoA_ variables for BOS
model4 <- glm(HomeWin ~ .,HoA_BOS,starts_with('Away_')) %>%
                dplyr::select(-Home_BOS,-Away_BOS))
summary(model4)