public async Task <UserTokenDto> Login(string email, string password) { const string message = "There was a problem logging in. Check your email and password or create an account."; var user = await _userManager.FindByEmailAsync(email); if (user == null) { throw new Exception(message); } var result = await _signInManager.CheckPasswordSignInAsync(user, password, false); if (!result.Succeeded) { throw new Exception(message); } var roles = await _userManager.GetRolesAsync(user); return(new UserTokenDto { Token = _tokenGenerator.Create(user, roles.ToList()), Customer = _mapper.Map <Customer, CustomerDto>(user.Customer) }); }
private string GenerateTokenForUser(User user) { var claims = new List <Claim> { new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()), new Claim(ClaimTypes.Name, user.Name), new Claim(ClaimTypes.Email, user.Email), }; claims.AddRange(user.Roles.Select(role => new Claim(ClaimTypes.Role, role.Role.Name))); return(_tokenFactory.Create(claims)); }
protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request , CancellationToken cancellationToken) { var current = request.RequestUri; var cacheKey = $"service_consul_url_{current.Host }"; try { var auth = request.Headers.Authorization; if (auth != null) { var tokenTxt = _tokenGenerator?.Create(); request.Headers.Authorization = new AuthenticationHeaderValue(auth.Scheme, tokenTxt); } var serverUrl = _memoryCache.GetOrCreate(cacheKey, entry => { entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(5); return(GetServiceAddress(current.Host)); }); request.RequestUri = new Uri($"{current.Scheme}://{serverUrl}{current.PathAndQuery}"); var responseMessage = await base.SendAsync(request, cancellationToken).ConfigureAwait(false); return(responseMessage); } catch (Exception ex) { _memoryCache.Remove(cacheKey); throw new Exception(ex.Message, ex); } finally { request.RequestUri = current; } }
protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request , CancellationToken cancellationToken) { var headers = request.Headers; var currentUri = request.RequestUri; var serviceAddressCacheKey = $"service_consul_url_{currentUri.Host }"; try { var auth = headers.Authorization; if (auth != null) { var tokenTxt = _tokenGenerator?.Create(); if (!string.IsNullOrEmpty(tokenTxt)) { request.Headers.Authorization = new AuthenticationHeaderValue(auth.Scheme, tokenTxt); } } var serviceUrls = await GetAllHealthServiceAddressAsync(currentUri.Host, serviceAddressCacheKey); var serviceUrl = GetServiceAddress(serviceUrls); if (serviceUrl.IsNullOrWhiteSpace()) { throw new ArgumentNullException($"{currentUri.Host} does not contain helath service address!"); } else { request.RequestUri = new Uri($"{currentUri.Scheme}://{serviceUrl}{currentUri.PathAndQuery}"); } //如果调用地址是https,使用http2 if (request.RequestUri.Scheme.Equals("https", StringComparison.OrdinalIgnoreCase)) { request.Version = new Version(2, 0); } #region 缓存处理 /* 这里高并发会有问题,需要优化,先注释 * if (request.Method == HttpMethod.Get) * { * var cache = headers.FirstOrDefault(x => x.Key == "Cache"); * * if (!string.IsNullOrWhiteSpace(cache.Key)) * { * int.TryParse(cache.Value.FirstOrDefault(), out int milliseconds); * * if (milliseconds > 0) * { * var cacheKey = request.RequestUri.AbsoluteUri.GetHashCode(); * * var existCache = _memoryCache.TryGetValue(cacheKey, out string content); * if (existCache) * { * var resp = new HttpResponseMessage * { * Content = new StringContent(content, Encoding.UTF8) * }; * * return resp.EnsureSuccessStatusCode(); * } * * //SendAsync异常(请求、超时异常),会throw * //服务端异常,不会抛出 * var responseResult = await base.SendAsync(request, cancellationToken).ConfigureAwait(false); * if (responseResult.IsSuccessStatusCode) * _memoryCache.Set(cacheKey, await responseResult.Content.ReadAsStringAsync(), TimeSpan.FromMilliseconds(milliseconds)); * * return responseResult; * } * } * } */ #endregion 缓存处理 var responseMessage = await base.SendAsync(request, cancellationToken); return(responseMessage); } catch (Exception) { _memoryCache.Remove(serviceAddressCacheKey); throw; } finally { request.RequestUri = currentUri; } }
protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { //如果调用地址是https,使用http2 if (request.RequestUri.Scheme.Equals("https", StringComparison.OrdinalIgnoreCase)) { request.Version = new Version(2, 0); } var headers = request.Headers; var auth = headers.Authorization; if (auth != null) { var tokenTxt = _tokenGenerator?.Create(); if (!string.IsNullOrEmpty(tokenTxt)) { request.Headers.Authorization = new AuthenticationHeaderValue(auth.Scheme, tokenTxt); } } #region 缓存处理 //if (request.Method == HttpMethod.Get) //{ // var cache = headers.FirstOrDefault(x => x.Key == "Cache"); // if (!string.IsNullOrWhiteSpace(cache.Key)) // { // int.TryParse(cache.Value.FirstOrDefault(), out int milliseconds); // if (milliseconds > 0) // { // var cacheKey = request.RequestUri.AbsoluteUri.GetHashCode(); // var existCache = _memoryCache.TryGetValue(cacheKey, out string content); // if (existCache) // { // var resp = new HttpResponseMessage // { // Content = new StringContent(content, Encoding.UTF8) // }; // return resp.EnsureSuccessStatusCode(); // } // //SendAsync异常(请求、超时异常),会throw // //服务端异常,不会抛出 // var responseResult = await base.SendAsync(request, cancellationToken).ConfigureAwait(true); // if (responseResult.IsSuccessStatusCode) // _memoryCache.Set(cacheKey, await responseResult.Content.ReadAsStringAsync(), TimeSpan.FromMilliseconds(milliseconds)); // return responseResult; // } // } //} #endregion 缓存处理 return(await base.SendAsync(request, cancellationToken)); }
protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { var headers = request.Headers; var auth = headers.Authorization; if (auth != null) { var tokenTxt = _tokenGenerator?.Create(); if (!string.IsNullOrEmpty(tokenTxt)) { request.Headers.Authorization = new AuthenticationHeaderValue(auth.Scheme, tokenTxt); } } if (request.Method == HttpMethod.Get) { var cache = headers.FirstOrDefault(x => x.Key == "Cache"); if (!string.IsNullOrWhiteSpace(cache.Key)) { int.TryParse(cache.Value.FirstOrDefault(), out int milliseconds); if (milliseconds > 0) { var cacheKey = request.RequestUri.AbsoluteUri.GetHashCode(); var existCache = _memoryCache.TryGetValue(cacheKey, out string content); if (existCache) { var resp = new HttpResponseMessage { Content = new StringContent(content, Encoding.UTF8) }; return(resp.EnsureSuccessStatusCode()); } //SendAsync异常(请求、超时异常),会throw //服务端异常,不会抛出 var responseResult = await base.SendAsync(request, cancellationToken).ConfigureAwait(false); if (responseResult.IsSuccessStatusCode) { _memoryCache.Set(cacheKey, await responseResult.Content.ReadAsStringAsync()); } return(responseResult); } } } var result = await base.SendAsync(request, cancellationToken).ConfigureAwait(false); return(result); //var content = await CacheManager.GetOrCreateAsync<string>(cacheKey, async entry => //{ // //SendAsync异常(请求、超时异常),会throw // //服务端异常,不会抛出 // var responseResult = await base.SendAsync(request, cancellationToken).ConfigureAwait(false); // if (!responseResult.IsSuccessStatusCode) // { // entry.AbsoluteExpirationRelativeToNow = null; // return null; // } // entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromMilliseconds(milliseconds); // return await responseResult.Content.ReadAsStringAsync(); //}); }
protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request , CancellationToken cancellationToken) { var headers = request.Headers; var currentUri = request.RequestUri; var serviceAddressCacheKey = $"service_consul_url_{currentUri.Host }"; try { var auth = headers.Authorization; if (auth != null) { var tokenTxt = _tokenGenerator?.Create(); if (!string.IsNullOrEmpty(tokenTxt)) { request.Headers.Authorization = new AuthenticationHeaderValue(auth.Scheme, tokenTxt); } } var serverUrl = _memoryCache.GetOrCreate(serviceAddressCacheKey, entry => { entry.AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(5); return(GetServiceAddress(currentUri.Host)); }); request.RequestUri = new Uri($"{currentUri.Scheme}://{serverUrl}{currentUri.PathAndQuery}"); //如果调用地址是https,使用http2 if (request.RequestUri.Scheme.Equals("https", StringComparison.OrdinalIgnoreCase)) { request.Version = new Version(2, 0); } #region 缓存处理 if (request.Method == HttpMethod.Get) { var cache = headers.FirstOrDefault(x => x.Key == "Cache"); if (!string.IsNullOrWhiteSpace(cache.Key)) { int.TryParse(cache.Value.FirstOrDefault(), out int milliseconds); if (milliseconds > 0) { var cacheKey = request.RequestUri.AbsoluteUri.GetHashCode(); var existCache = _memoryCache.TryGetValue(cacheKey, out string content); if (existCache) { var resp = new HttpResponseMessage { Content = new StringContent(content, Encoding.UTF8) }; return(resp.EnsureSuccessStatusCode()); } //SendAsync异常(请求、超时异常),会throw //服务端异常,不会抛出 var responseResult = await base.SendAsync(request, cancellationToken).ConfigureAwait(false); if (responseResult.IsSuccessStatusCode) { _memoryCache.Set(cacheKey, await responseResult.Content.ReadAsStringAsync(), TimeSpan.FromMilliseconds(milliseconds)); } return(responseResult); } } } #endregion var responseMessage = await base.SendAsync(request, cancellationToken).ConfigureAwait(false); return(responseMessage); } catch (Exception ex) { _memoryCache.Remove(serviceAddressCacheKey); throw new Exception(ex.Message, ex); } finally { request.RequestUri = currentUri; } }