コード例 #1
0
ファイル: LoginService.cs プロジェクト: allenhan/Green
        public bool SingIn(Customer cusomer)
        {
            var now = DateTime.UtcNow.ToLocalTime();
            var ticket = new FormsAuthenticationTicket(
                1 /*version*/,
                cusomer.UserName,
                now,
                now.Add(TimeSpan.FromMinutes(1)),
                false,
                cusomer.UserName,
                FormsAuthentication.FormsCookiePath);

            var encryptedTicket = FormsAuthentication.Encrypt(ticket);

            var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
            cookie.HttpOnly = true;
            if (ticket.IsPersistent)
            {
                cookie.Expires = ticket.Expiration;
            }
            cookie.Secure = FormsAuthentication.RequireSSL;
            cookie.Path = FormsAuthentication.FormsCookiePath;
            if (FormsAuthentication.CookieDomain != null)
            {
                cookie.Domain = FormsAuthentication.CookieDomain;
            }
            HttpContext.Current.Response.Cookies.Add(cookie);
            cacheCustomer = cusomer;
            return true;
        }
コード例 #2
0
ファイル: LoginService.cs プロジェクト: allenhan/Green
        public virtual Customer GetAuthenticatedCustomerFromTicket(FormsAuthenticationTicket ticket)
        {
            if (ticket == null)
                throw new ArgumentNullException("ticket");

            var username = ticket.UserData;

            if (String.IsNullOrWhiteSpace(username))
                return null;
            var customer = authService.GetCustomer(username);
            cacheCustomer = customer;
            return cacheCustomer;
        }
コード例 #3
0
ファイル: LoginService.cs プロジェクト: allenhan/Green
        public Customer GetCustomer()
        {
            try
            {
                if (this.cacheCustomer != null)
                    return this.cacheCustomer;
                if (HttpContext.Current == null || HttpContext.Current.Request == null || !(HttpContext.Current.Request.IsAuthenticated) || !(HttpContext.Current.User.Identity is FormsIdentity))
                    return null;

                var formsIdentity = (FormsIdentity)HttpContext.Current.User.Identity;
                var customer = GetAuthenticatedCustomerFromTicket(formsIdentity.Ticket);
                this.cacheCustomer = customer;
                return this.cacheCustomer;
            }
            catch (Exception)
            {
                return null;
            }
        }
コード例 #4
0
ファイル: LoginService.cs プロジェクト: allenhan/Green
 public void SingOut()
 {
     FormsAuthentication.SignOut();
     cacheCustomer = null;
 }