Example #1
0
 private string GetGravatarUri(User user)
 {
     if (String.IsNullOrEmpty(user.Gravatar))
     {
         return user.Email.ToGravatarUri(140);
     }
     else
     {
         return user.Gravatar.ToGravatarUri(140);
     }
 }
Example #2
0
        public ActionResult Authenticate(string returnUrl)
        {
            var response = OpenId.GetResponse();

            if (response == null)
            {
                Identifier identifier;

                if (Identifier.TryParse(Request.Form["openid_identifier"], out identifier))
                {
                    try
                    {
                        var request = OpenId.CreateRequest(identifier);

                        var fetchRequest = new FetchRequest();
                        fetchRequest.Attributes.AddRequired(WellKnownAttributes.Contact.Email);
                        fetchRequest.Attributes.AddRequired(WellKnownAttributes.Name.First);
                        fetchRequest.Attributes.AddRequired(WellKnownAttributes.Name.Last);

                        request.AddExtension(fetchRequest);

                        return request.RedirectingResponse.AsActionResult();
                    }
                    catch (ProtocolException ex)
                    {
                        TempData["Message"] = ex.Message;
                        return RedirectToAction("Login");
                    }
                }

                TempData["Message"] = "Invalid identifier";
                return RedirectToAction("Login");
            }

            switch (response.Status)
            {
                case AuthenticationStatus.Authenticated:
                    var fetchResponse = response.GetExtension<FetchResponse>();
                    var displayName = "";
                    var email = "";

                    if (fetchResponse != null)
                    {
                        displayName = String.Format("{0} {1}",
                            fetchResponse.GetAttributeValue(WellKnownAttributes.Name.First),
                            fetchResponse.GetAttributeValue(WellKnownAttributes.Name.Last));
                        email = fetchResponse.GetAttributeValue(WellKnownAttributes.Contact.Email);
                    }

                    var user = _userRepository.GetByClaimedIdentifier(response.ClaimedIdentifier);

                    // Create user if he/she does not exist in the database.
                    if (user == null)
                    {
                        user = new User
                        {
                            ClaimedIdentifier = response.ClaimedIdentifier,
                            DisplayName = displayName,
                            Email = email
                        };

                        _userRepository.Save(user);
                    }

                    // If the stored display name is not equal to the friendly one from the provider,
                    // overwrite it.
                    if (!String.Equals(user.DisplayName, displayName)
                        || !String.Equals(user.Email, email))
                    {
                        user.DisplayName = displayName;
                        user.Email = email;

                        _userRepository.Update(user);
                    }

                    // Set auth cookie
                    FormsAuthentication.SetAuthCookie(user.Id.ToString(), false);

                    if (!String.IsNullOrEmpty(returnUrl))
                    {
                        return Redirect(returnUrl);
                    }

                    return RedirectToAction("Profile", new {id = user.Id});

                case AuthenticationStatus.Canceled:
                    TempData["Message"] = "Canceled at provider";
                    return RedirectToAction("Login");

                case AuthenticationStatus.Failed:
                    TempData["Message"] = response.Exception.Message;
                    return RedirectToAction("Login");
            }

            return HttpNotFound();
        }
Example #3
0
        public ActionResult Edit(int? id, User user)
        {
            if (User.Identity.IsAuthenticated)
            {
                var dbUser = _userRepository.GetById(id.Value);
                dbUser.Gravatar = user.Gravatar;
                _userRepository.Update(dbUser);

                return RedirectToAction("Settings", new {id = id});
            }
            else
            {
                return HttpNotFound();
            }
        }