public override async Task<HttpResponseMessage> ExecuteAsync(System.Web.Http.Controllers.HttpControllerContext controllerContext, System.Threading.CancellationToken cancellationToken)
        {
            var request = controllerContext.Request;
            object obj;
            if (request.Properties.TryGetValue(UGConstants.HTTPHeaders.TOKEN_NAME, out obj))
            {
                UGToken = obj as UGToken;
                if (UGToken != null)
                {
                    UGUser = IoTUserManager.GetUser(UGToken);

                    if (UGUser != null && string.IsNullOrWhiteSpace(UGUser.JsonProfile))
                    {
                        //Get data attach (List<int> storesId) - List store by User
                        var lst = new List<int>() { 1 };

                        Profile p = new Profile();
                        p.Stores = lst;
                        //Update profile
                        IoTUserManager.UpdateProfile(UGUser.UserName, p.SerializeJson());
                    }
                }
            }

            return await base.ExecuteAsync(controllerContext, cancellationToken);
        }
Beispiel #2
0
        public User GetBySSOUser(IPrincipal user)
        {
            var u = new User()
            {
                Id = user.GetUserId(),
                UserName = user.GetUserName(),
                Email = user.GetEmail(),
                PhoneNumber = user.GetPhoneNumber().Count > 0 ? user.GetPhoneNumber().First() : string.Empty
            };

            return u;
        }
        public override async Task<HttpResponseMessage> ExecuteAsync(System.Web.Http.Controllers.HttpControllerContext controllerContext, System.Threading.CancellationToken cancellationToken)
        {
            var request = controllerContext.Request;
            var headerUsername = request.Headers.GetValues(UGConstants.ClaimTypes.PreferredUserName);
            if (headerUsername != null && headerUsername.Count() > 0)
            {
                UGUser = IoTUserManager.GetUserCache(headerUsername.First());

                if (UGUser != null && string.IsNullOrWhiteSpace(UGUser.JsonProfile))
                {
                    //Get data attach (List<int> storesId) - List store by User
                    var lst = new List<int>() { 1 };

                    Profile p = new Profile();
                    p.Stores = lst;
                    //Update profile
                    IoTUserManager.UpdateProfile(UGUser.UserName, p.SerializeJson());
                }
            }

            return await base.ExecuteAsync(controllerContext, cancellationToken);
        }
Beispiel #4
0
        public async Task<User> GetBySSOIoTUserAsync(string accessToken)
        {
            var client = new UserInfoClient(
                new Uri(UGConstants.SSO.UserInfoEndpoint),
                accessToken);

            var response = await client.GetAsync();
            var user = new User();
            if (response.Claims != null)
            {
                foreach (var ui in response.Claims)
                {
                    if (ui.Item1 == UGConstants.ClaimTypes.Subject)
                        user.Id = ui.Item2;
                    if (ui.Item1 == UGConstants.ClaimTypes.PreferredUserName)
                        user.UserName = ui.Item2;
                }
            }
            return user;
        }
Beispiel #5
0
        public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
        {
            if (User.Identity.IsAuthenticated)
            {
                return RedirectToAction("Index", "Manage");
            }

            if (ModelState.IsValid)
            {
                // Get the information about the user from the external login provider
                var info = await AuthenticationManager.GetExternalLoginInfoAsync();
                if (info == null)
                {
                    return View("ExternalLoginFailure");
                }
                var user = new User { UserName = model.Email, Email = model.Email };
                var result = await UserManager.CreateAsync(user);
                if (result.Succeeded)
                {
                    result = await UserManager.AddLoginAsync(user.Id, info.Login);
                    if (result.Succeeded)
                    {
                        await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
                        return RedirectToLocal(returnUrl);
                    }
                }
                AddErrors(result);
            }

            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }
Beispiel #6
0
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new User { UserName = model.Email, Email = model.Email };
                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    //  Comment the following line to prevent log in until the user is confirmed.
                    //await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false);
                    
                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    //string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    //var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    //await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");
                    string callbackUrl = await SendEmailConfirmationTokenAsync(user.Id, "Confirm your account");

                    // Uncomment to debug locally 
                    // TempData["ViewBagLink"] = callbackUrl;

                    ViewBag.Message = "Check your email and confirm your account, you must be confirmed "
                                    + "before you can log in.";

                    return View("Info");
                    //return RedirectToAction("Index", "Home");
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }
Beispiel #7
0
 private async Task SignInAsync(User user, bool isPersistent)
 {
     AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie, DefaultAuthenticationTypes.TwoFactorCookie);
     AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = isPersistent }, await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie));
 }
Beispiel #8
0
        protected User SetUserToCache(User userDB, string cacheKey)
        {
            //Groups of Application
            var lstGrp = _grpManager.FindByUserName(userDB.UserName).Distinct();
            userDB.Groups = new List<Group>(lstGrp);
            //Permission of Application
            var lstPer = _perManager.FindPermissionsByUserName(userDB.UserName).Distinct();
            userDB.Permissions = new List<Permission>(lstPer);

            //cache
            CacheUser.Set<User>(cacheKey, userDB);
            return userDB;
        }