public ActionResult register(Models.APIRegisterModel model) { if (ModelState.IsValid) { using (var daApiKeys = new DataAccess.DataAccessObjects.APIKey()) { if (daApiKeys.LoadAPIKey(model.Email) != null) { ModelState.AddModelError("Email", "An API key is already registered to this email. To ask for your API key to be resent <a href=\"/developer/recover\">click here</a>"); return(View(model)); } else { daApiKeys.SaveAPIKey(new DataAccess.APIKey() { Email = model.Email, RequestCount = 0 }); Code.Utilities.SendAPIKey(model.Email); model.confirmation = true; } } } return(View(model)); }
public static bool SendAPIKey(string email) { using (var daAPIKeys = new DataAccess.DataAccessObjects.APIKey()) { var key = daAPIKeys.LoadAPIKey(email); if (key != null) { var msg = new SendGridMessage(); msg.SetFrom("*****@*****.**"); msg.SetTemplateId("292fe174-26c0-429e-83f6-7443d85eb63e"); //string content = File.ReadAllText(HttpContext.Current.Server.MapPath("~/Views/Shared/Other/Email.html")); //content = content.Replace("{email}", email).Replace("{apiKey}", key.APIKey1.ToString()); //msg.AddContent(MimeType.Html, content); //msg.Personalizations = new List<Personalization>() { // new Personalization() // { // Tos = new List<EmailAddress>() { new EmailAddress(email) } // } //}; msg.AddTo(key.Email); msg.AddSubstitutions(new Dictionary <string, string>() { { "-email-", email }, { "-apiKey-", key.APIKey1.ToString() } }); string SG_APIKey = ConfigurationManager.AppSettings["SendGridApiKey"]; var client = new SendGridClient(SG_APIKey); var response = client.SendEmailAsync(msg).Result; } return(key != null); } }
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext) { if (actionExecutedContext.Request.Headers.TryGetValues("X-API-KEY", out var headerValue)) { using (var daApiKey = new DataAccess.DataAccessObjects.APIKey()) { var key = headerValue.FirstOrDefault(); if (key != null) { var APIKey = daApiKey.LoadAPIKey(new Guid(key)); APIKey.RequestCount += 1; APIKey.LastRequestDt = DateTime.Now; daApiKey.SaveAPIKey(APIKey); } } } }
public override void OnActionExecuting(HttpActionContext actionContext) { bool validKey = false; if (actionContext.Request.Headers.TryGetValues("X-API-KEY", out var headerValue)) { using (var daApiKey = new DataAccess.DataAccessObjects.APIKey()) { var key = headerValue.FirstOrDefault(); if (key != null) { var APIKey = daApiKey.LoadAPIKey(new Guid(key)); validKey = APIKey != null; } } } if (!validKey) { actionContext.Response = actionContext.Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "You must include a valid API key by adding the 'X-API-KEY' header in the request."); } }