Exemple #1
0
        public ActionResult ExternalLoginCallback(string returnUrl, string provider)
        {
            var claims         = ClaimsPrincipal.Current.Claims.ToList();
            var nameIdentifier = claims.FirstOrDefault(x => x.Type.Equals(UrnLookup.GetNamespaceForName(provider), StringComparison.InvariantCultureIgnoreCase));
            var uidIdentifier  = claims.FirstOrDefault(x => x.Type.Equals(UrnLookup.GetNamespaceForId(), StringComparison.InvariantCultureIgnoreCase));

            if (nameIdentifier == null || uidIdentifier == null)
            {
                string debugInfo = "";
                foreach (Claim claim in claims)
                {
                    debugInfo += $"{claim.Type} : {claim.Value}\n";
                }

                throw new InvalidOperationException("The OAuth provider didn't provide a name or nameidentifier:\n " + debugInfo);
            }

            string id   = uidIdentifier.Value;
            string name = nameIdentifier.Value;

            string userData = JsonConvert.SerializeObject(new UserContext()
            {
                FullName = name, Id = id
            });
            string encryptedData = FormsAuthentication.Encrypt(new FormsAuthenticationTicket(1, "Syringe", DateTime.Now, DateTime.UtcNow.AddDays(1), true, userData));

            // Add UserData to the forms auth cookie by setting the cookie manually.
            Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encryptedData)
            {
                Expires = DateTime.Now.AddDays(1)
            });

            return(Redirect(returnUrl));
        }
        public ActionResult ExternalLoginCallback(string returnUrl, string provider)
        {
            var claims         = ClaimsPrincipal.Current.Claims.ToList();
            var nameIdentifier = claims.FirstOrDefault(x => x.Type.Equals(UrnLookup.GetNamespaceForName(provider), StringComparison.InvariantCultureIgnoreCase));
            var uidIdentifier  = claims.FirstOrDefault(x => x.Type.Equals(UrnLookup.GetNamespaceForId(), StringComparison.InvariantCultureIgnoreCase));

            string id   = uidIdentifier == null ? "Anon" : uidIdentifier.Value;
            string name = nameIdentifier == null ? "Anon" : uidIdentifier.Value;

            string userData = JsonConvert.SerializeObject(new UserContext()
            {
                FullName = name, Id = id
            });
            string encryptedData = FormsAuthentication.Encrypt(new FormsAuthenticationTicket(1, "Syringe", DateTime.Now, DateTime.UtcNow.AddDays(1), true, userData));

            // Add UserData to the forms auth cookie by setting the cookie manually.
            Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encryptedData)
            {
                Expires = DateTime.Now.AddDays(1)
            });

            return(Redirect(returnUrl));
        }
 public void GetNamespaceForName_should_return_default_namespace()
 {
     Assert.AreEqual("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name", UrnLookup.GetNamespaceForName(It.IsAny <string>()));
 }
 public void GetNamespaceForName_should_return_github_namespace(string provider)
 {
     Assert.AreEqual("urn:github:name", UrnLookup.GetNamespaceForName(provider));
 }
 public void GetNamespaceForId_should_return_correct_schema()
 {
     Assert.AreEqual("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier", UrnLookup.GetNamespaceForId());
 }