public IdentityResponseModel GetCurrentIdentity()
        {
            IdentityResponseModel result = null;
            var identity = ServiceContext.Identity;

            if (identity != null && identity.IsAuthenticated)
            {
                result = new IdentityResponseModel()
                {
                    UserId      = ServiceContext.CurrentUserName,
                    Roles       = identity.Claims.Where(c => c.Type == identity.RoleClaimType).Select(c => c.Value.ToLower()).ToArray(),
                    AccessGroup = identity.Claims.SingleOrDefault(c => c.Type == ApplicationUser.AccessGroupClaimType)?.Value?.ToLower()
                };
            }
            return(result);
        }
Ejemplo n.º 2
0
        public async Task Invoke(HttpContext context)
        {
            string token = context.Request.Headers["Authorization"];

            if (!string.IsNullOrEmpty(token))
            {
                token = token.Replace("Bearer", "");
                token = token.Replace("bearer", "");
                token = token.Trim();
                IdentityServerCall    identityServerCall = new IdentityServerCall();
                IdentityResponseModel responseBody       = await identityServerCall.ValidateToken(token, identityClientModel.IdentityServerUrl);

                context.Response.ContentType = "application/json";
                if (responseBody.code != "IDS00")
                {
                    context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                    await context.Response.WriteAsync(JsonConvert.SerializeObject(responseBody,
                                                                                  new JsonSerializerSettings()
                    {
                        NullValueHandling = NullValueHandling.Ignore
                    }));
                }
                else
                {
                    IdentityProfile       identityProfile = responseBody.data;// JsonConvert.DeserializeObject<IdentityProfile>(responseBody.data);
                    List <ClaimsIdentity> claims          = new List <ClaimsIdentity>()
                    {
                        new ClaimsIdentity(new List <Claim>()
                        {
                            new Claim(ClaimTypes.NameIdentifier, identityProfile.Id),
                            new Claim(ClaimTypes.Authentication, "true"),
                            new Claim(ClaimTypes.Email, string.IsNullOrEmpty(identityProfile.email)?string.Empty:identityProfile.email),
                            new Claim(ClaimTypes.MobilePhone, string.IsNullOrEmpty(identityProfile.phoneNumber)? string.Empty: identityProfile.phoneNumber),
                            new Claim(ClaimTypes.Name, $"{identityProfile.lastName} {identityProfile.otherNames??string.Empty}"),
                            new Claim(ClaimTypes.Surname, identityProfile.lastName)
                        }, "Basic")
                    };

                    context.User = new ClaimsPrincipal(claims);
                    await next(context);
                }
            }
            else
            {
                await next(context);
            }
        }
Ejemplo n.º 3
0
        public async Task <IdentityResponseModel> ValidateToken(string token, string identityServerUrl)
        {
            HttpClient httpClient = new HttpClient();

            try
            {
                httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
                HttpResponseMessage response = await httpClient.GetAsync($"{identityServerUrl}");

                IdentityResponseModel responseBody = await response.Content.ReadAsAsync <IdentityResponseModel>();

                httpClient.Dispose();
                return(responseBody);
            }
            catch (Exception)
            {
                httpClient.Dispose();
                throw new IdentityValidationException("An error occur while trying to validate identity");
            }
        }
Ejemplo n.º 4
0
        public IdentityResponseModel GetIdentity()
        {
            IdentityResponseModel result = userService.GetCurrentIdentity();

            return(result);
        }