示例#1
0
        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            //var isUserId = false; //是否Password是UserId
            var headers      = context.Request.Headers;
            var isDomainUser = true;

            if (headers.ContainsKey(DomainKey))
            {
                var v = headers.GetValues(DomainKey).FirstOrDefault();
                if (v != null)
                {
                    var notDomain = String.Equals(false.ToString(), v, StringComparison.CurrentCultureIgnoreCase);
                    if (notDomain)
                    {
                        isDomainUser = false;
                    }
                }
            }
            else
            {
                var qs = context.Request.Query;
                if (qs != null)
                {
                    var domain = qs.Any(c => c.Key.ToUpper() == "DOMAIN");
                    if (domain)
                    {
                        var v         = qs.FirstOrDefault(c => c.Key.ToUpper() == "DOMAIN").Value.FirstOrDefault();
                        var notDomain = String.Equals(false.ToString(), v, StringComparison.CurrentCultureIgnoreCase);
                        if (notDomain)
                        {
                            isDomainUser = false;
                        }
                    }
                }
            }

            var scope         = context.OwinContext.GetAutofacLifetimeScope();
            var userManager   = scope.Resolve <UserManager <User, long> >();
            var vaultService  = scope.Resolve <IVaultServerService>();
            var mfuserService = scope.Resolve <IMFUserService>();
            var userService   = scope.Resolve <IUserService>();
            var res           = await Task.Run(() => AuthUtility.Login(context.UserName, context.Password, isDomainUser, userManager
                                                                       , vaultService, mfuserService, userService));

            if (!string.IsNullOrEmpty(res.Error))
            {
                context.SetError(res.Error);
                return;
            }
            var user = res.User;

            var oAuthIdentity = await userManager.CreateIdentityAsync(user,
                                                                      OAuthDefaults.AuthenticationType);

            var cookiesIdentity = await userManager.CreateIdentityAsync(user,
                                                                        CookieAuthenticationDefaults.AuthenticationType);

            var data = new Dictionary <string, string>
            {
                { "userName", user.UserName },
                { "disabled", user.Disabled.ToString() },
                { "email", user.Email }
            };

            var properties = CreateProperties(data);
            var ticket     = new AuthenticationTicket(oAuthIdentity, properties);

            context.Validated(ticket);
            context.Request.Context.Authentication.SignIn(cookiesIdentity);
            user.LastIpAddress    = context.Request.RemoteIpAddress;
            user.LastLoginDateUtc = DateTime.UtcNow;
            await userManager.UpdateAsync(user);

            //if (!isUserId)
            //{
            DBWorldCache.Add(user.Id.ToString(), context.Password);
            //}
        }
示例#2
0
        public async Task <ActionResult> LogOn(LoginViewModel model, string returnUrl)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            // 这不会计入到为执行帐户锁定而统计的登录失败次数中
            // 若要在多次输入错误密码的情况下触发帐户锁定,请更改为 shouldLockout: true

            var userRes = await Task.Run(() => AuthUtility.Login(model.UsernameOrEmail, model.Password, model.IsDomainUser,
                                                                 _userManager, _vaultserverService, _mfuserService, _userService));

            if (userRes.User != null)
            {
                var user = userRes.User;

                await _signInManager.SignInAsync(user, model.RememberMe, false);

                //   Log.InfoFormat("登录账户:{0}, 公司:{1},部门:{2}", user.UserName, user.Company.Name, user.Department.Name);
                DBWorldCache.Add(user.Id.ToString(), model.Password);
                //check whether current user can connect contractor vault.
                try {
                    //分包商菜单处理,使用缓存
                    var thevault = _vaultRepository.Table.FirstOrDefault(c => c.CloudId == 3);
                    var app      = MFServerUtility.ConnetToMfApp(user, model.Password, thevault.Server);
                    var vault    = app.LogInToVault(thevault.Guid);
                    DBWorldCache.Add(user.Id.ToString() + "canManageContractor", "true");
                    //  SimpleLog(user.FullName + "can view contractors,id=" + user.Id);
                }
                catch (Exception) { }
                try
                {
                    var vs     = _vaultserverService.GetServer();
                    var mfapp  = MFServerUtility.ConnectToServer(vs);
                    var vaults = mfapp.GetOnlineVaults();
                    foreach (VaultOnServer vaultOnServer in vaults)
                    {
                        //  SimpleLog(vaultOnServer.Name + "  task check!");
                        Vault vault;
                        try
                        {
                            vault = vaultOnServer.LogIn();
                        }
                        catch (Exception)
                        {
                            SimpleLog(string.Format("Info: vault:{0},{1}", vaultOnServer.Name, "no right"));
                            continue;
                        }
                        try
                        {
                            var havetask = CheckTaskInOneVault(vault, user.Id);
                            if (havetask)
                            {
                                DBWorldCache.Add(user.Id.ToString() + "havetask", havetask.ToString());
                                //  SimpleLog(vault.Name + " havetask !");
                                break;
                            }
                            //  SimpleLog(vault.Name + " have no task !");
                        }
                        catch (Exception ex)
                        {
                            SimpleLog("havetask check error:" + ex.Message);
                        }
                    }
                    foreach (VaultOnServer vaultOnServer in vaults)
                    {
                        //  SimpleLog(vaultOnServer.Name + "  notice check!");
                        Vault vault;
                        try
                        {
                            vault = vaultOnServer.LogIn();
                        }
                        catch (Exception)
                        {
                            SimpleLog(string.Format("Info: vault:{0},{1}", vaultOnServer.Name, "no right"));
                            continue;
                        }
                        try
                        {
                            var havenotice = CheckNoticeInOneVault(vault, user.Id);

                            if (havenotice)
                            {
                                DBWorldCache.Add(user.Id.ToString() + "havenotice", havenotice.ToString());
                                SimpleLog(vault.Name + " havenotice !");
                                break;
                            }
                            //  SimpleLog(vault.Name + " have no notice !");
                        }
                        catch (Exception ex)
                        {
                            SimpleLog("havenotice check error:" + ex.Message);
                        }
                    }
                }
                catch (Exception ex)
                {
                    SimpleLog("check whether current user can connect contractor vault." + ex.Message);
                }
                return(RedirectToLocal(returnUrl));
            }
            else
            {
                ModelState.AddModelError("", userRes.Error);
            }

            return(View(model));
        }