public IActionResult Execute(T input) { SetCurrentUser(); try { _authorization.Authorize(CurrentUser, input); } catch (UnauthorizedAccessException) { return(new UnauthorizedResult()); } return(OperationBody(input)); }
private BaseModel ConfirmEmail(string email, string mailKey) { var model = new BaseModel(); try { var user = context.Login.Single(x => x.Email.Equals(email)); if (user.MailKey.Equals(mailKey)) { user.IsActive = true; context.SaveChanges(); var token = authenticator.Authorize(email, true); model.token = token; return(model); } model.isError = true; model.errorDescription = "Mail anahtarı hatalı"; return(model); } catch (Exception ex) { model.isError = true; model.errorDescription = "Beklenmeyen hata"; return(model); } }
public void Authorize(HttpContext context, IDictionary <string, string> query) { if (query.ContainsKey("grant_type")) { string grantType = query["grant_type"]; switch (grantType) { case "refresh_token": //if an application wants to exchange an refresh token for an access token if (_refreshTokenAuthorization != null) { _refreshTokenAuthorization.Authorize(context, query); } else { InvalidAuthorizationResponse errorResponse = new InvalidAuthorizationResponse(); errorResponse.Error = InvalidAuthorizationResponse.UNSUPPORTED_GRANT_TYPE; errorResponse.Error_description = "The grant type 'refresh_token' is not supported by this application"; //TODO: add URL //write payload context.Response.Payload.Write(JsonSerializer.SerializeJson(errorResponse)); context.Response.Headers.Set("Content-Type", MimeType.APPLICATION_JSON); context.Response.Status = HttpStatus.BadRequest; } break; case "authorization_code": //if an application wants to exchange an authorization code for an access token if (_authorizationCodeAuthorization != null) { _authorizationCodeAuthorization.Authorize(context, query); } else { InvalidAuthorizationResponse errorResponse = new InvalidAuthorizationResponse(); errorResponse.Error = InvalidAuthorizationResponse.UNSUPPORTED_GRANT_TYPE; errorResponse.Error_description = "The grant type 'authorization_code' is not supported by this application"; //TODO: add URL //write payload context.Response.Payload.Write(JsonSerializer.SerializeJson(errorResponse)); context.Response.Headers.Set("Content-Type", MimeType.APPLICATION_JSON); context.Response.Status = HttpStatus.BadRequest; } break; case "password": //if an application wants to exchange the user's name and password for an access token if (_passwordAuthorization != null) { _passwordAuthorization.Authorize(context, query); } else { InvalidAuthorizationResponse errorResponse = new InvalidAuthorizationResponse(); errorResponse.Error = InvalidAuthorizationResponse.UNSUPPORTED_GRANT_TYPE; errorResponse.Error_description = "The grant type 'password' is not supported by this application"; //TODO: add URL //write payload context.Response.Payload.Write(JsonSerializer.SerializeJson(errorResponse)); context.Response.Headers.Set("Content-Type", MimeType.APPLICATION_JSON); context.Response.Status = HttpStatus.BadRequest; } break; case "client_credentials": //if an application wants to exchange the client's credentials for an access token, not on behalf of a user if (_clientCredentialsAuthorization != null) { _clientCredentialsAuthorization.Authorize(context, query); } else { InvalidAuthorizationResponse errorResponse = new InvalidAuthorizationResponse(); errorResponse.Error = InvalidAuthorizationResponse.UNSUPPORTED_GRANT_TYPE; errorResponse.Error_description = "The grant type 'client_credentials' is not supported by this application"; //TODO: add URL //write payload context.Response.Payload.Write(JsonSerializer.SerializeJson(errorResponse)); context.Response.Headers.Set("Content-Type", MimeType.APPLICATION_JSON); context.Response.Status = HttpStatus.BadRequest; } break; default: InvalidAuthorizationResponse errResponse = new InvalidAuthorizationResponse(); errResponse.Error = InvalidAuthorizationResponse.UNSUPPORTED_GRANT_TYPE; errResponse.Error_description = "The grant type '" + grantType + "' is not supported by this application"; //TODO: add URL //write payload context.Response.Payload.Write(JsonSerializer.SerializeJson(errResponse)); context.Response.Headers.Set("Content-Type", MimeType.APPLICATION_JSON); context.Response.Status = HttpStatus.BadRequest; break; } } else if (query.ContainsKey("response_type")) { } else { //create invalid request InvalidAuthorizationResponse errorResponse = new InvalidAuthorizationResponse(); errorResponse.Error = InvalidAuthorizationResponse.INVALID_REQUEST; errorResponse.Error_description = "The grant type or response type is missing. The request must include the query parameter 'grant_type' or 'response_type."; //TODO: add URL //write payload context.Response.Payload.Write(JsonSerializer.SerializeJson(errorResponse)); context.Response.Headers.Set("Content-Type", MimeType.APPLICATION_JSON); context.Response.Status = HttpStatus.BadRequest; } }
public void Authorization(string authLogin, string authPassword) { CurrentUser = Authorizator.Authorize(authLogin, authPassword); ReadPersonData(); }
public BaseController() { try { // // process authentication if a provider is configured // // string authNProviderName = ConfigurationManager.AppSettings["AuthenticationProvider"]; if (!string.IsNullOrEmpty(authNProviderName)) { Type authNProviderType = Type.GetType(authNProviderName); if (authNProviderType == null) { SendError(401, "Unable to load authentication provider."); } // only send 408 error when the request is ajax and authenticated user is unknown if (System.Web.HttpContext.Current.Request.Headers["X-Requested-With"] == "XMLHttpRequest" && SessionState.Get(System.Web.HttpContext.Current.Session.SessionID, AUTH_USER) == null) { SendError(408, "Session timed out."); return; } IAuthentication authNProvider = (IAuthentication)Activator.CreateInstance(authNProviderType); _authenticatedUser = authNProvider.Authenticate(System.Web.HttpContext.Current.Session); SessionState.Set(System.Web.HttpContext.Current.Session.SessionID, AUTH_USER, _authenticatedUser); if (System.Web.HttpContext.Current.Response.IsRequestBeingRedirected) { return; } if (string.IsNullOrEmpty(_authenticatedUser)) { SendError(401, "Authentication failed."); } // // process authorization if a provider is configured // string authZProviderName = ConfigurationManager.AppSettings["AuthorizationProvider"]; if (!string.IsNullOrEmpty(authZProviderName)) { Type authZProviderType = Type.GetType(authZProviderName); if (authZProviderType == null) { SendError(401, "Unable to load authorization provider."); } IAuthorization authZProvider = (IAuthorization)Activator.CreateInstance(authZProviderType); bool authorized = authZProvider.Authorize(System.Web.HttpContext.Current.Session, "adapterAdmins", _authenticatedUser); if (!authorized) { SendError(401, "User [" + _authenticatedUser + "] not authorized."); } } // // get authorization headers if a provider is configured // try { string headersProviderName = ConfigurationManager.AppSettings["AuthHeadersProvider"]; if (!string.IsNullOrEmpty(headersProviderName)) { Type headersProviderType = Type.GetType(headersProviderName); if (headersProviderType == null) { SendError(401, "Unable to load auth header provider."); } IAuthHeaders headersProvider = (IAuthHeaders)Activator.CreateInstance(headersProviderType); _authHeaders = headersProvider.Get(_authenticatedUser); } } catch (Exception e) { _logger.Error("Error getting authorization headers: " + e); throw e; } } } catch (Exception e) { _logger.Error("Authentication error: " + e.Message + ": " + e.StackTrace.ToString()); SendError(401, e.ToString()); } }
public async Task <IHttpActionResult> GetAll() { IHttpActionResult result = null; try { // Authorize var authorized = await _authorization.Authorize("Basic", "Product", "GET"); if (!authorized) { throw new Exception("Unauthorized user access."); } // Get Products var products = await _productFactory.GetProducts(); if (products != null && products.Items != null && products.Items.Count > 0) { result = Ok(products); } else { var response = new HttpResponseMessage(HttpStatusCode.NoContent); result = ResponseMessage(response); } } catch (Exception ex) { result = InternalServerError(ex); } return(result); }