Example #1
0
        public async Task Invoke(HttpContext context)
        {
            try
            {
                if (context.Request.Path.Value.Equals("/") ||
                    context.Request.Path.Value.Contains("/api/account/FilePath") ||
                    context.Request.Path.Value.Contains("/api/account/refreshtoken") ||
                    context.Request.Path.Value.Contains("/api/subscriber") ||
                    context.Request.Path.Value.Contains("/api/account/login") ||
                    context.Request.Path.Value.StartsWith("/wwwroot/") ||
                    context.Request.Path.Value.Contains("/api/alert/addiotalert") ||
                    context.Request.Path.Value.Contains("/api/account/adminlogin"))    // Nikunj
                {
                    await _next.Invoke(context);

                    return;
                }

                SolutionConfiguration.CompanyId = Guid.Parse(context.Request.Headers["company-id"]);
                if (SolutionConfiguration.CompanyId == null || SolutionConfiguration.CompanyId == Guid.Empty)
                {
                    context.Response.StatusCode = 401;
                    await context.Response.WriteAsync("Header key is missing!");
                }

                if (context.User != null && context.User.Claims != null)
                {
                    SolutionConfiguration.BearerToken = context.User.Claims.Where(c => c.Type == "IOT_CONNECT").FirstOrDefault().Value;
                    if (!string.IsNullOrWhiteSpace(SolutionConfiguration.BearerToken) && SolutionConfiguration.BearerToken.Equals("AdminUser"))
                    {
                        SolutionConfiguration.CurrentUserId = Guid.Parse(context.User.Claims.Where(c => c.Type == "CURRENT_USERID").FirstOrDefault().Value);
                    }
                    else
                    {
                        JwtSecurityTokenHandler hand = new JwtSecurityTokenHandler();
                        var tokenS    = hand.ReadJwtToken(SolutionConfiguration.BearerToken);
                        var jsonValue = tokenS.Claims?.SingleOrDefault(p => p.Type == "user")?.Value;
                        Entity.UserDetail userDetail = Newtonsoft.Json.JsonConvert.DeserializeObject <Entity.UserDetail>(jsonValue);
                        SolutionConfiguration.CurrentUserId = Guid.Parse(userDetail.Id);
                        SolutionConfiguration.SolutionId    = Guid.Parse(userDetail.SolutionGuid);
                        SolutionConfiguration.EntityGuid    = Guid.Parse(userDetail.EntityGuid);
                    }
                }

                await _next.Invoke(context);

                return;
            }
            catch (Exception ex)
            {
                ex.ToString();
                throw;
            }
        }
        public Entity.ActionStatus Login(Entity.LoginRequest request)
        {
            Entity.ActionStatus result = new Entity.ActionStatus(true);
            try
            {
                IOT.DataResponse <IOT.LoginResult> loginResult = _iotConnectClient.Login.Login(new IOT.LoginModel()
                {
                    UserName = request.Username,
                    Password = request.Password
                }).Result;

                result.Success = loginResult.status;
                if (loginResult != null && loginResult.status)
                {
                    JwtSecurityTokenHandler hand = new JwtSecurityTokenHandler();
                    var tokenS    = hand.ReadJwtToken(loginResult.data.access_token);
                    var jsonValue = tokenS.Claims?.SingleOrDefault(p => p.Type == "user")?.Value;
                    Entity.UserDetail userDetail = Newtonsoft.Json.JsonConvert.DeserializeObject <Entity.UserDetail>(jsonValue);
                    var user = _userRepository.GetByUniqueId(r => r.Guid == Guid.Parse(userDetail.Id));

                    if (user == null)
                    {
                        return(new Entity.ActionStatus()
                        {
                            Success = false, Message = "User does not exist in Solution"
                        });
                    }
                    userDetail.FullName = user.FirstName.ToString() + " " + user.LastName.ToString();

                    result.Data = new Entity.LoginResponse
                    {
                        status        = loginResult.data.status,
                        data          = loginResult.data.data,
                        message       = loginResult.message,
                        token_type    = loginResult.data.token_type,
                        access_token  = loginResult.data.access_token,
                        refresh_token = loginResult.data.refresh_token,
                        expires_in    = loginResult.data.expires_in,
                        UserDetail    = userDetail
                    };
                }
                else
                {
                    result.Message = loginResult.message;
                }
            }
            catch (Exception ex)
            {
                _logger.ErrorLog(ex, $"UserManager.Login {ex.Message}");
                return(new Entity.ActionStatus(false, ex.Message));
            }
            return(result);
        }
        public async Task Invoke(HttpContext context)
        {
            try
            {
                if (context.Request.Path.Value.Equals("/") ||
                    context.Request.Path.Value.Contains("/api/account/FilePath") ||
                    context.Request.Path.Value.Contains("/api/account/refreshtoken") ||
                    context.Request.Path.Value.Contains("/api/subscriber") ||
                    context.Request.Path.Value.Contains("/api/account/login") ||
                    context.Request.Path.Value.Contains("/api/account/identity") ||
                    context.Request.Path.Value.StartsWith("/wwwroot/") ||
                    context.Request.Path.Value.Contains("/api/alert/addiotalert") ||
                    context.Request.Path.Value.Contains("/api/account/adminlogin"))     // Nikunj
                {
                    await _next.Invoke(context);

                    return;
                }
                if (!context.Request.Headers.ContainsKey("company-id") || string.IsNullOrWhiteSpace(context.Request.Headers["company-id"]))
                {
                    context.Response.StatusCode = 401;
                    await context.Response.WriteAsync("Header key is missing!");
                }
                SolutionConfiguration.CompanyId = Guid.Parse(context.Request.Headers["company-id"]);
                if (SolutionConfiguration.CompanyId == null || SolutionConfiguration.CompanyId == Guid.Empty)
                {
                    context.Response.StatusCode = 401;
                    await context.Response.WriteAsync("Header key is missing!");
                }

                if (context.User != null && context.User.Claims != null)
                {
                    SolutionConfiguration.BearerToken = context.Request.Headers["Authorization"].ToString().Replace("Bearer", "").Trim();
                    Claim admin = context.User.Claims.Where(c => c.Type == "IOT_CONNECT").FirstOrDefault();
                    if (admin != null && admin.Value.Equals("AdminUser"))
                    {
                        SolutionConfiguration.CurrentUserId = Guid.Parse(context.User.Claims.Where(c => c.Type == "CURRENT_USERID").FirstOrDefault().Value);
                    }
                    else if (admin != null)
                    {
                        JwtSecurityTokenHandler hand = new JwtSecurityTokenHandler();
                        var BearerToken = admin.Value;
                        var tokenS      = hand.ReadJwtToken(BearerToken);
                        var jsonValue   = tokenS.Claims?.SingleOrDefault(p => p.Type == "user")?.Value;

                        Entity.UserDetail userDetail = Newtonsoft.Json.JsonConvert.DeserializeObject <Entity.UserDetail>(jsonValue);
                        SolutionConfiguration.BearerToken   = BearerToken;
                        SolutionConfiguration.CurrentUserId = Guid.Parse(userDetail.Id);
                        SolutionConfiguration.SolutionId    = Guid.Parse(userDetail.SolutionGuid);
                        SolutionConfiguration.EntityGuid    = Guid.Parse(userDetail.EntityGuid);
                        SolutionConfiguration.CompanyId     = Guid.Parse(userDetail.CompanyId);
                    }
                    else
                    {
                        Entity.ActionStatus loginResponse = _userService.Identity(SolutionConfiguration.BearerToken);
                        if (loginResponse != null && loginResponse.Success)
                        {
                            IoTConnect.Model.IdentityResult responsedata = loginResponse.Data;
                            SolutionConfiguration.CurrentUserId = Guid.Parse(responsedata.data.userGuid);
                            SolutionConfiguration.SolutionId    = Guid.Parse(responsedata.data.solutionGuid);
                            SolutionConfiguration.EntityGuid    = Guid.Parse(responsedata.data.entityGuid);
                            SolutionConfiguration.CompanyId     = Guid.Parse(responsedata.data.companyGuid);
                        }
                        else
                        {
                            context.Response.StatusCode = 401;
                            await context.Response.WriteAsync(loginResponse.Message);
                        }
                    }
                    //SolutionConfiguration.BearerToken = context.User.Claims.Where(c => c.Type == "IOT_CONNECT").FirstOrDefault().Value;
                    //if (!string.IsNullOrWhiteSpace(SolutionConfiguration.BearerToken) && SolutionConfiguration.BearerToken.Equals("AdminUser"))
                    //{
                    //    SolutionConfiguration.CurrentUserId = Guid.Parse(context.User.Claims.Where(c => c.Type == "CURRENT_USERID").FirstOrDefault().Value);
                    //}
                    //else
                    //{
                    //    JwtSecurityTokenHandler hand = new JwtSecurityTokenHandler();
                    //    var tokenS = hand.ReadJwtToken(SolutionConfiguration.BearerToken);
                    //    var jsonValue = tokenS.Claims?.SingleOrDefault(p => p.Type == "user")?.Value;
                    //    Entity.UserDetail userDetail = Newtonsoft.Json.JsonConvert.DeserializeObject<Entity.UserDetail>(jsonValue);
                    //    SolutionConfiguration.CurrentUserId = Guid.Parse(userDetail.Id);
                    //    SolutionConfiguration.SolutionId = Guid.Parse(userDetail.SolutionGuid);
                    //    SolutionConfiguration.EntityGuid = Guid.Parse(userDetail.EntityGuid);
                    //}
                }

                await _next.Invoke(context);

                return;
            }
            catch (Exception ex)
            {
                ex.ToString();
                throw;
            }
        }