private Response Login(object arg) { var user = (string)this.Request.Form.Name; var password = (string)this.Request.Form.Password; if (string.IsNullOrEmpty(user) || string.IsNullOrEmpty(password)) { return(new Response { StatusCode = HttpStatusCode.Unauthorized }); } var userDao = new UserDAOImpl(); if (userDao.Find(new User { Name = user, Password = password })) { var apiKeyDao = new ApiKeyDAOImpl(); var apiKey = apiKeyDao.FindByName(user); if (apiKey is null) { apiKey = new ApiKey { Name = user, Key = CreateApiKey(user, out CreateTime), CreateTime = this.CreateTime }; apiKeyDao.Add(apiKey); } else { if (!apiKey.IsValidKey()) { apiKey.Key = CreateApiKey(user, out CreateTime); apiKey.CreateTime = this.CreateTime; apiKeyDao.Update(apiKey); } } return(this.Response.AsJson(new { ApiKey = apiKey.Key, Redirect = "main" })); } return(new Response { StatusCode = HttpStatusCode.Unauthorized }); }
private StatelessAuthenticationConfiguration GetConfiguration() { return(new StatelessAuthenticationConfiguration(ctx => { var isJsonRequest = false; var apiKey = RequestHelper.GetApiKey(ctx); if (string.IsNullOrEmpty(apiKey)) { ctx.Response = new Response { StatusCode = HttpStatusCode.Unauthorized }; return null; } var apikeyDao = new ApiKeyDAOImpl(); var key = apikeyDao.FindByKey(apiKey); if (key != null) { if (!key.IsValidKey()) { key.Key = null; key.CreateTime = DateTime.MinValue; apikeyDao.Update(key); } else { return key; } } if (key is null && isJsonRequest) { ctx.Response = Response.AsJson(new { message = "Invalid Key" }, HttpStatusCode.BadRequest); return null; } ctx.Response = new RedirectResponse("user", RedirectResponse.RedirectType.SeeOther); return null; })); }
private StatelessAuthenticationConfiguration GetConfiguration() { return(new StatelessAuthenticationConfiguration(ctx => { var apiKey = RequestHelper.GetApiKey(ctx); if (string.IsNullOrEmpty(apiKey)) { return null; } var apikeyDao = new ApiKeyDAOImpl(); var key = apikeyDao.FindByKey(apiKey); if (key != null && key.IsValidKey()) { ctx.Response = new RedirectResponse("main", RedirectResponse.RedirectType.SeeOther); } return key; })); }