Exemple #1
0
        private async Task SignIn(IdentityAccess identity)
        {
            var token         = identity.AccessToken.Split('.');
            var base64Content = Convert.FromBase64String(
                token[1].Replace('-', '+').Replace('_', '/').PadRight(4 * ((token[1].Length + 3) / 4), '=')
                );

            var user = JsonSerializer.Deserialize <AccessTokenUser>(base64Content);

            var claims = new List <Claim>
            {
                new Claim(ClaimTypes.NameIdentifier, user.nameid),
                new Claim(ClaimTypes.Name, user.unique_name),
                new Claim(ClaimTypes.Email, user.email),
                new Claim("access_token", identity.AccessToken)
            };

            var claimsIdentity = new ClaimsIdentity(
                claims, CookieAuthenticationDefaults.AuthenticationScheme);

            var authProperties = new AuthenticationProperties
            {
                IssuedUtc = DateTime.UtcNow.AddHours(1)
            };

            await HttpContext.SignInAsync(
                CookieAuthenticationDefaults.AuthenticationScheme,
                new ClaimsPrincipal(claimsIdentity),
                authProperties);
        }
Exemple #2
0
        //Create Access
        public bool CreateAccess(IdentityAccess identity)
        {
            var isSuccess = false;

            try
            {
                using (var conn = new SqlConnection(_connectionString))
                {
                    var parameters = new Dictionary <string, object>
                    {
                        { "@AccessName", identity.AccessName },
                        { "@Description", identity.Description }
                    };

                    var query = @"INSERT INTO aspnetaccess(Id,AccessName,Active,Description) values(NEWID(),@AccessName,1,@Description)";
                    MsSqlHelper.ExecuteNonQuery(conn, query, parameters);

                    isSuccess = true;
                }
            }
            catch (Exception ex)
            {
                isSuccess = false;
            }

            return(isSuccess);
        }
Exemple #3
0
        public bool CheckAccessDuplicate(IdentityAccess identity)
        {
            var existed = false;

            try
            {
                using (var conn = new SqlConnection(_connectionString))
                {
                    var parameters = new Dictionary <string, object>
                    {
                        { "@AccessId", identity.Id },
                        { "@AccessName", identity.AccessName }
                    };

                    var query  = @"SELECT 1 FROM aspnetaccess WHERE 1=1 AND AccessName = @AccessName AND Id != @AccessId";
                    var result = MsSqlHelper.ExecuteScalar(conn, CommandType.Text, query, parameters);

                    if (Convert.ToBoolean(result))
                    {
                        existed = true;
                    }
                }
            }
            catch (Exception ex)
            {
                existed = false;
            }

            return(existed);
        }
Exemple #4
0
        //Update Access
        public bool UpdateAccess(IdentityAccess identity)
        {
            var isSuccess = false;

            try
            {
                using (var conn = new SqlConnection(_connectionString))
                {
                    var parameters = new Dictionary <string, object>
                    {
                        { "@AccessId", identity.Id },
                        { "@AccessName", identity.AccessName },
                        { "@Description", identity.Description }
                    };

                    var query = @"Update aspnetaccess SET AccessName = @AccessName, Description = @Description WHERE 1=1 AND Id = @AccessId;";
                    MsSqlHelper.ExecuteNonQuery(conn, query, parameters);

                    isSuccess = true;
                }
            }
            catch (Exception ex)
            {
                isSuccess = false;
            }

            return(isSuccess);
        }
Exemple #5
0
        public ActionResult Create(AccessViewModel model)
        {
            var result = false;

            if (ModelState.IsValid)
            {
                var strError       = string.Empty;
                var accessIdentity = new IdentityAccess {
                    Id = model.AccessId, AccessName = model.AccessName, Description = model.AccessDesc
                };
                try
                {
                    var isDuplicated = _identityStore.CheckAccessDuplicate(accessIdentity);
                    if (isDuplicated)
                    {
                        this.AddNotification(string.Format("Could not create access due to the access [{0}] is existed", model.AccessName), NotificationType.ERROR);
                        return(RedirectToAction("Index"));
                    }

                    result = _identityStore.CreateAccess(accessIdentity);
                    if (result)
                    {
                        this.AddNotification("The access [" + model.AccessName + "] is created succesfully", NotificationType.SUCCESS);

                        //Write log
                        var activityText = "Create new access [Name: {0}]";
                        activityText = string.Format(activityText, model.AccessName);
                        WriteActivityLog(activityText, ActivityLogType.CreateAccess, model.AccessId, TargetObjectType.Access);

                        return(RedirectToAction("Index"));
                    }
                    else
                    {
                        this.AddNotification("Could not create access due to database exception occurred", NotificationType.ERROR);
                    }
                }
                catch (Exception ex)
                {
                    strError = string.Format("Could not CreateAccess because: {0}", ex.ToString());
                    logger.Error(strError);
                    this.AddNotification(strError, NotificationType.ERROR);
                }
            }

            return(RedirectToAction("Index"));
        }
Exemple #6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="HubsterAuthClient" /> class.
 /// </summary>
 /// <param name="onAuthRequest">The on authentication request.</param>
 /// <param name="hostUrl">The host URL.</param>
 public HubsterAuthClient(Func <HubsterAuthClient, IdentityResponse <IdentityToken> > onAuthRequest, string hostUrl = "https://identity.hubster.io")
 {
     _identityAccess = new IdentityAccess(hostUrl);
     _onAuthRequest  = onAuthRequest;
 }