Пример #1
0
      public void HasSameValuesAfterToStringAndParse()
      {
         CookieUserData data = new CookieUserData(_id, _displayName, _twoRoles);
         string asString = data.ToString();

         CookieUserData parsedData = CookieUserData.Parse(asString);

         Assert.AreEqual(_id, parsedData.Id);
         Assert.AreEqual(_displayName, parsedData.DisplayName);
         Assert.AreEqual(2, parsedData.Roles.Count());

         Assert.Contains(_roleOne, parsedData.Roles);
         Assert.Contains(_roleTwo, parsedData.Roles);
      }
Пример #2
0
      /// <summary>
      /// This authenticates a user based on a response from an openid provider.
      /// </summary>
      private ActionResult AuthenticateUser(IAuthenticationResponse response)
      {
         if (response.Status != AuthenticationStatus.Authenticated)
            throw new ArgumentException("The response status must be 'Authenticated'. (" + response.Status.ToString() + ")", "response");

         TadmapDb db = new TadmapDb();

         var user = db.UserOpenIds.Where(u => u.OpenIdUrl == response.ClaimedIdentifier.ToString()).SingleOrDefault();
         Guid userId;

         if (user != null)
         {
            userId = user.UserId;
         }
         else
         {
            userId = CreateNewUser(response.ClaimedIdentifier.ToString());
         }

         var roles = from role in db.UserRoles
                     where role.UserId == userId
                     select role.Role;

         CookieUserData userData = new CookieUserData(
            userId,
            response.FriendlyIdentifierForDisplay.ToString(),
            roles.ToArray()
         );
         
         FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
           1,
           response.ClaimedIdentifier.ToString(),
           DateTime.Now,
           DateTime.Now.AddHours(5),
           false,
           userData.ToString()
         );

         // Encrypt the ticket.
         string encTicket = FormsAuthentication.Encrypt(ticket);

         // Create the cookie.
         Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));

         return RedirectToAction("Index", new { controller = "Home" });
      }
Пример #3
0
 public void Has_ToString()
 {
    CookieUserData data = new CookieUserData(_id, _displayName, _twoRoles);
    string asString = data.ToString();
    Assert.IsNotNull(asString);
 }
Пример #4
0
      /// <summary>
      /// This authenticates a user based on a response from an openid provider.
      /// </summary>
      private ActionResult AuthenticateUser(IAuthenticationResponse response)
      {
         if (response.Status != AuthenticationStatus.Authenticated)
            throw new ArgumentException("The response status must be 'Authenticated'. (" + response.Status.ToString() + ")", "response");

         var user = UserRepository.GetAllUsers().Where(u => u.OpenIds.Contains(response.ClaimedIdentifier.ToString())).SingleOrDefault();

         if (user == null)
         {
            user = new Model.User.User
            {
               Name = response.ClaimedIdentifier.ToString(),
               OpenIds = new List<string>{ response.ClaimedIdentifier.ToString() },
               Roles = new List<string>{ TadmapRoles.Collector }
            };

            UserRepository.Save(user);
         }

         CookieUserData userData = new CookieUserData(
            user.Id,
            response.FriendlyIdentifierForDisplay.ToString(),
            user.Roles.ToArray()
         );
         
         FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
           1,
           response.ClaimedIdentifier.ToString(),
           DateTime.Now,
           DateTime.Now.AddHours(5),
           false,
           userData.ToString()
         );

         // Encrypt the ticket.
         string encTicket = FormsAuthentication.Encrypt(ticket);

         // Create the cookie.
         Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));

         return RedirectToAction("Index", new { controller = "Home" });
      }