2014-07-01 3 views
0

인증이 필요한 일부 휴식 서비스로 앱을 연결하려고하는데 몇 가지 문제가 있습니다. 내가 HttpClient 클래스를 사용하고 로그인이나 가입과 같은 인증을 필요로하지 않는 서비스와 잘 작동합니다. 문제는 내가 AuthenticationHeaderValue 개체에 스키마를 지정해야하며이 스키마가 헤더에 들어간다는 것입니다. 헤더의 결과는 다음과 같습니다. "Authorization : Authorization a81b4974-f328-44e0-901a-95e29fb672aa : sKJQgOqJswCLHlibsMGRYZb/dlkyPzVnvs9uqqx5ToM ="서버가 찾고있는 것은 "Authorization : a81b4974-f328-44e0-901a-95e29fb672aa : sKJQgOqJswCLHlibsMGRYZb/dlkyPzVnvs9uqqx5ToM = " 여기 내가 사용하고있는 코드는 다음과 같습니다 REST 서비스에 대한 인증 문제가`WP8` 앱의`HttpClient`를 사용합니다.

public async void addProject(string name) 
{ 
    string service = "/service/project/add"; 
    string serviceURL = "/pwpcloud"+service; 
    StringBuilder parametersBuilder = new StringBuilder(); 

    parametersBuilder.Append("{\"name\":\"" + name + "\","); 
    parametersBuilder.Append("\"description\":\"" + "projectDescription" + "\","); 
    parametersBuilder.Append("\"sparsePath\":\"" + "fasdd" + "\","); 
    parametersBuilder.Append("\"densePath\":\"" + "asdf" + "\","); 
    parametersBuilder.Append("\"matchFormat\":\"" + "asdf" + "\","); 
    parametersBuilder.Append("\"metadata\":\"" + "aaaaa" + "\","); 
    parametersBuilder.Append("\"user\":\"" + mLoginData.getUserID() + "\"}"); 

    string parameters = parametersBuilder.ToString(); 

    HttpClient restClient = new HttpClient(); 
    restClient.BaseAddress = new Uri(mBaseURL); 
    restClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); 

    //falta la autenticacion 
    setAuthorization(restClient, service, WEBSERVICE_REQUEST_TYPE_POST); 
    HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, serviceURL); 
    req.Content = new StringContent(parameters, Encoding.UTF8, "application/json"); 
    HttpResponseMessage response = null; 
    string responseBodyAsText = ""; 
    try 
    { 
     response = await restClient.SendAsync(req); 
     response.EnsureSuccessStatusCode(); 
     responseBodyAsText = await response.Content.ReadAsStringAsync(); 
    } 
    catch (HttpRequestException e) 
    { 
     string ex = e.Message; 
    } 
} 

public void setAuthorization(HttpClient request, string service, int reqType, string token, string userID) 
{ 
    //Date OK 
    string date = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ"); 

    //nonce OK 
    Random random = new Random(); 
    String nonce = ""; 
    for (int i = 0; i < 5; i++) 
    { 
     string randomValue = (1111 + random.Next() % (9999 - 1111)).ToString(); 
     nonce = nonce + randomValue; 
    } 
    //type OK 
    string type = ""; 
    if (reqType == WEBSERVICE_REQUEST_TYPE_GET) 
    { 
     type = "GET"; 
    } 
    else 
    { 
     type = "POST"; 
    } 

    //Authorization: 
    string stringToHash = token + ":" + service + "," + type + "," + date + "," + nonce; 
    string authorizationCrypted = encryptStringSHA256(stringToHash); 
    //string authorization = userID + ":" + authorizationCrypted; 
    request.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Authorization", string.Format("{0}:{1}", userID, authorizationCrypted)); 

    request.DefaultRequestHeaders.Add("x-rest-date", date); 
    request.DefaultRequestHeaders.Add("nonce", nonce); 
} 
public static string encryptStringSHA256(string stringToEncrypt) 
{ 
    var hash = new SHA256Managed(); 

    byte[] stringHash = StringToAscii(stringToEncrypt); 
    byte[] encryptedString = hash.ComputeHash(stringHash); 
    return Convert.ToBase64String(encryptedString); 
} 

//Metodo para convertir string a bytes ascii NO IMPLEMENTADO POR DEFECTO EN EL API DE WINDOWS PHONE 
public static byte[] StringToAscii(string s) 
{ 
    byte[] retval = new byte[s.Length]; 
    for (int ix = 0; ix < s.Length; ++ix) 
    { 
     char ch = s[ix]; 
     if (ch <= 0x7f) retval[ix] = (byte)ch; 
     else retval[ix] = (byte)'?'; 
    } 

    return retval; 
} 

당신의 도움에 감사드립니다.

+0

누구나 나를 도와 줄 수 있습니까? –

답변

0

HttpClient.DefaultRequestHeaders.TryAddWithoutValidation("Key",value) 메서드를 사용하여 해결했습니다. 다음 코드입니다.

public void setAuthorization(HttpClient request, string service, int reqType, string token, string userID) 
{ 
    //Date OK 
    string date = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ"); 

    //nonce OK 
    Random random = new Random(); 
    String nonce = ""; 
    for (int i = 0; i < 5; i++) 
    { 
     string randomValue = (1111 + random.Next() % (9999 - 1111)).ToString(); 
     nonce = nonce + randomValue; 
    } 
    //type OK 
    string type = ""; 
    if (reqType == WEBSERVICE_REQUEST_TYPE_GET) 
    { 
     type = "GET"; 
    } 
    else 
    { 
     type = "POST"; 
    } 

    //Authorization: 
    string stringToHash = token + ":" + service + "," + type + "," + date + "," + nonce; 
    string authorizationCrypted = encryptStringSHA256(stringToHash); 
    string authorization = userID + ":" + authorizationCrypted; 
    request.DefaultRequestHeaders.TryAddWithoutValidation("x-rest-date", date); 
    request.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", authorization); 
    request.DefaultRequestHeaders.TryAddWithoutValidation("nonce", nonce); 
} 
관련 문제