protected void CleanupForLogout(string cookieName, string cookieProtectionApp)
        {
            Guid tokenId = GetTokenIdFromCookie(cookieName, cookieProtectionApp);

            if (!Guid.Empty.Equals(tokenId))
            {
                using (IBGoodMusicRepository repo = new BGoodMusic.EFDAL.BGoodMusicDBContext())
                {
                    repo.RemoveUserInfoItem(tokenId);
                }
                RemoveCookie(cookieName);
            }
        }
Example #2
0
        public ActionResult Index()
        {
            IBGoodMusicRepository    repo     = new BGoodMusic.EFDAL.BGoodMusicDBContext();
            List <RehearsalListItem> itemList = new List <RehearsalListItem>();

            foreach (var r in repo.GetRehearsals().ToList())
            {
                itemList.Add(new RehearsalListItem
                {
                    Id       = r.Id,
                    Date     = r.Date,
                    Duration = r.Duration,
                    Location = r.Location,
                    Time     = r.Time
                });
            }
            return(View(itemList));
        }
        protected string RefreshToken(string cookieName, string cookieProtectionApp, string tokenProtectionApp)
        {
            Guid tokenId = GetTokenIdFromCookie(cookieName, cookieProtectionApp);

            if (!Guid.Empty.Equals(tokenId))
            {
                using (IBGoodMusicRepository repo = new BGoodMusic.EFDAL.BGoodMusicDBContext())
                {
                    var userInfo = repo.GetUserInfoItem(tokenId);
                    if (userInfo != null)
                    {
                        string token = MachineKeyHelper.UnprotectTo1252String(userInfo.Token, this.User, tokenProtectionApp);
                        return(token);
                    }
                }
            }
            return(null);
        }
        protected bool GetRefreshTokenAndSave(string code,
                                              string adfsAuthUserId,
                                              Uri callbackUri,
                                              string cookieName,
                                              string protectionApp,
                                              StringBuilder msg)
        {
            var ctx      = new AuthenticationContext(Startup.Config.ADFS_URL_adfs, false);
            var cred     = new ClientCredential(adfsAuthUserId, "NotASecret");
            var response = ctx.AcquireTokenByAuthorizationCode(code, callbackUri, cred);

            if (response == null)
            {
                msg.AppendLine("Response null");
            }
            else
            {
                msg.AppendLine("Got response");
                if (!string.IsNullOrWhiteSpace(response.AccessToken))
                {
                    msg.AppendLine(" - Got Access Token");
                }
                if (string.IsNullOrWhiteSpace(response.RefreshToken))
                {
                    msg.AppendLine(" - No Refresh Token");
                }
                else
                {
                    string nameId         = null;
                    string protectedToken = null;
                    Guid   tokenId        = Guid.Empty;
                    msg.AppendFormat(" - Got Refresh Token len={1}{0} -- starts with \"{2}\"{0}",
                                     Environment.NewLine,
                                     response.RefreshToken.Length,
                                     response.RefreshToken.Substring(0, 10));
                    SSC.ClaimsPrincipal cp = this.User as SSC.ClaimsPrincipal;
                    if (cp != null)
                    {
                        nameId = cp.GetNameIdentiferValue();
                        if (!string.IsNullOrWhiteSpace(nameId))
                        {
                            protectedToken = MachineKeyHelper.Protect1252(response.RefreshToken, cp, protectionApp);
                            string unprotectedToken = MachineKeyHelper.UnprotectTo1252String(protectedToken, this.User, protectionApp);
                            if (response.RefreshToken != unprotectedToken)
                            {
                                msg.AppendFormat(" - Protect / Unprotect different.{0}... token len = {1}, start=\"{2}\"{0}... token len = {3}, start = \"{4}\"{0}",
                                                 Environment.NewLine,
                                                 response.RefreshToken.Length,
                                                 response.RefreshToken.Substring(0, 20),
                                                 unprotectedToken.Length,
                                                 unprotectedToken.Substring(0, 20));
                            }
                            if (string.IsNullOrWhiteSpace(nameId))
                            {
                                msg.Append(" ** could not get Name Identifier **");
                            }
                            if (string.IsNullOrWhiteSpace(protectedToken))
                            {
                                msg.Append(" ** could not protect token **");
                            }

                            if (!string.IsNullOrWhiteSpace(nameId))
                            {
                                using (IBGoodMusicRepository repo = new BGoodMusic.EFDAL.BGoodMusicDBContext())
                                {
                                    tokenId = repo.AddNewUserInfo(nameId, protectedToken);
                                    if (Guid.Empty.Equals(tokenId))
                                    {
                                        msg.Append(" ** failed to create UserInfo **");
                                    }
                                }
                            }
                            if (!Guid.Empty.Equals(tokenId))
                            {
                                var tokenIdCookie = new HttpCookie(cookieName)
                                {
                                    Domain   = "localhost",
                                    HttpOnly = true,
                                    Path     = "/demo/",
                                    Secure   = true,
                                    Value    = MachineKeyHelper.Protect(tokenId.ToByteArray(), cp, protectionApp)
                                };
                                Response.Cookies.Add(tokenIdCookie);
                                return(true);
                            }
                        }
                    }
                }
            }
            return(false);
        }