/// <summary> /// Basic Authenticator /// </summary> /// <param name="authHeader"></param> private MeisterStatus RunAsBasicAuthentication(string authHeader) { string encodedUsernamePassword = authHeader.Trim(); Encoding encoding = Encoding.GetEncoding("iso-8859-1"); string usernamePassword = encoding.GetString(Convert.FromBase64String(encodedUsernamePassword)); int separatorIndex = usernamePassword.IndexOf(':'); username = usernamePassword.Substring(0, separatorIndex); password = usernamePassword.Substring(separatorIndex + 1); IAuthenticator authenticator = Client.Authenticator = new HttpBasicAuthenticator(username, password); if (authenticator != null) { var request = new RestRequest(Method.GET); DoResourceAllocation(request, metadata); request.AddHeader(csrf, "Fetch"); IRestResponse response = Client.Execute(request); MeisterStatus = new MeisterStatus(response, request.Resource); if (HttpResponseInValidRange(response.StatusCode)) { IsAutheticated = true; } return(MeisterStatus); } else { throw new MeisterException(resourceManager.GetString("BadAuthetication", CultureInfo.InvariantCulture)); } }
/// <summary> /// OAuth 2.0 /// </summary> /// <param name="authHeader"></param> /// <returns></returns> private MeisterStatus RunAsOAuth2Authentication(string authHeader) { string encodedUsernamePassword = authHeader.Substring("Token ".Length).Trim(); Encoding encoding = Encoding.GetEncoding("iso-8859-1"); string accesstokentype = encoding.GetString(Convert.FromBase64String(encodedUsernamePassword)); int seperatorIndex = accesstokentype.IndexOf(':'); accessToken = accesstokentype.Substring(0, seperatorIndex); tokenType = accesstokentype.Substring(seperatorIndex + 1); IAuthenticator authenticator = Client.Authenticator = new OAuth2AuthorizationRequestHeaderAuthenticator(accessToken, tokenType); if (authenticator != null) { var request = new RestRequest(Method.GET); DoResourceAllocation(request, metadata); request.AddHeader("Authorization", string.Format("bearer {0}", accessToken)); request.AddHeader("Accept", "application/json"); IRestResponse response = Client.Execute(request); MeisterStatus = new MeisterStatus(response, request.Resource); if (HttpResponseInValidRange(response.StatusCode)) { IsAutheticated = true; } return(MeisterStatus); } else { throw new MeisterException(resourceManager.GetString("InvalidOauth", CultureInfo.InvariantCulture)); } }
private dynamic ExecuteRequest <REQ, RES>(Resource <REQ, RES> resource, string endPoint, REQ request) { try { return(resource.Execute(endPoint, request)); } catch (Exception) { return(null); } finally { MeisterStatus = resource.MeisterStatus; } }
public bool Authenticate(MeisterSupport.Languages language = MeisterSupport.Languages.CultureBased) { MeisterExtensions = MeisterSupport.MeisterExtensions.RemoveNullsAndEmptyArrays; MeisterOptions = MeisterSupport.MeisterOptions.None; if (IsODataV4) { MeisterOptions = MeisterSupport.MeisterOptions.UseODataV4; } MeisterRuntimeOptios = MeisterSupport.RuntimeOptions.ExecuteSync; MeisterLanguageSetting = language; Resource <dynamic, dynamic> resource = BuildResource <dynamic, dynamic>(); MeisterStatus = resource.Authenticate(); if (IsStatusOK()) { return(true); } else { return(false); } }