示例#1
0
        public async Task<ActionResult> DoAsync(SignInInfo model)
        {
            if (!this.ModelState.IsValid)
            {
                return this.InvalidModelState(this.ModelState);
            }

            try
            {
                bool isValid =
                    await this.CheckPasswordAsync(this.Tenant, model.Email, model.Password).ConfigureAwait(false);

                if (!isValid)
                {
                    return new HttpStatusCodeResult(HttpStatusCode.Forbidden);
                }

                var result = await DAL.SignIn.DoAsync(this.Tenant, model.Email, model.OfficeId, this.RemoteUser.Browser,
                            this.RemoteUser.IpAddress, model.Culture.Or("en-US")).ConfigureAwait(false);

                string key = "access_tokens_" + this.Tenant;
                var factory = new DefaultCacheFactory();
                factory.Remove(key);

                return await this.OnAuthenticatedAsync(result, model).ConfigureAwait(true);
            }
            catch (DbException ex)
            {
                Log.Information(ex.Message);
                return this.AccessDenied();
            }
        }
        protected async Task<ActionResult> OnAuthenticatedAsync(LoginResult result, SignInInfo model = null)
        {
            if (!result.Status)
            {
                await Task.Delay(new Random().Next(1, 5)*1000).ConfigureAwait(false);
                return new HttpStatusCodeResult(HttpStatusCode.Forbidden, JsonConvert.SerializeObject(result));
            }

            Guid? applicationId = null;

            if (model != null)
            {
                applicationId = model.ApplicationId;
            }

            var loginView = await AppUsers.GetCurrentAsync(this.Tenant, result.LoginId).ConfigureAwait(false);

            var manager = new Provider(this.Tenant, applicationId, result.LoginId, loginView.UserId, loginView.OfficeId);
            var token = manager.GetToken();
            string domain = TenantConvention.GetDomain();

            await
                AccessTokens.SaveAsync(this.Tenant, token, this.RemoteUser.IpAddress, this.RemoteUser.UserAgent)
                    .ConfigureAwait(true);

            var cookie = new HttpCookie("access_token")
            {
                Value = token.ClientToken,
                HttpOnly = true,
                Expires = token.ExpiresOn.DateTime
            };

            //localhost cookie is not supported by most browsers.
            if (domain.ToLower() != "localhost")
            {
                cookie.Domain = domain;
            }

            this.Response.Cookies.Add(cookie);
            return this.Ok(token.ClientToken);
        }
        protected ActionResult OnAuthenticated(LoginResult result, SignInInfo model = null)
        {
            if (!result.Status)
            {
                Thread.Sleep(new Random().Next(1, 5)*1000);
                return new HttpStatusCodeResult(HttpStatusCode.Forbidden, JsonConvert.SerializeObject(result));
            }

            Guid? applicationId = null;

            if (model != null)
            {
                applicationId = model.ApplicationId;
            }

            var manager = new Provider(AppUsers.GetCatalog(), applicationId, result.LoginId);
            var token = manager.GetToken();
            string domain = DbConvention.GetDomain();

            AccessTokens.Save(token, this.RemoteUser.IpAddress, this.RemoteUser.UserAgent);

            var cookie = new HttpCookie("access_token")
            {
                Value = token.ClientToken,
                HttpOnly = true,
                Secure = true,
                Expires = token.ExpiresOn
            };

            //localhost cookie is not supported by most browsers.
            if (domain.ToLower() != "localhost")
            {
                cookie.Domain = domain;
            }

            this.Response.Cookies.Add(cookie);
            return Json(token.ClientToken);
        }
示例#4
0
        public ActionResult Do(SignInInfo model)
        {
            if (!ModelState.IsValid)
            {
                return new HttpStatusCodeResult(HttpStatusCode.Forbidden);
            }

            try
            {
                bool isValid = this.CheckPassword(model.Email, model.Password);

                if (!isValid)
                {
                    return new HttpStatusCodeResult(HttpStatusCode.Forbidden);
                }

                var result = DAL.SignIn.Do(model.Email, model.OfficeId, this.RemoteUser.Browser, this.RemoteUser.IpAddress, model.Culture);
                return this.OnAuthenticated(result, model);
            }
            catch (NpgsqlException)
            {
                return Json("Access is denied.");
            }
        }