Пример #1
0
        public ActionResult GoogleLogOn(string returnUrl)
        {
            var response = openid.GetResponse();
            if (response == null)
            {
                // Stage 2: user submitting Identifier
                Identifier id;
                if (Identifier.TryParse(Request.Form["openid_identifier"], out id))
                {
                    try
                    {

                        var request = openid.CreateRequest(Request.Form["openid_identifier"]);
                        var fetch = new FetchRequest();

                        fetch.Attributes.Add(new AttributeRequest(WellKnownAttributes.Contact.Email, true));
                        request.AddExtension(fetch);

                        return request.RedirectingResponse.AsActionResult();
                    }
                    catch (ProtocolException ex)
                    {
                        ViewData["Message"] = ex.Message;
                        return View("Login");
                    }
                }
                else
                {
                    ViewData["Message"] = "Invalid identifier";
                    return View("Login");
                }
            }
            else
            {
                // Stage 3: OpenID Provider sending assertion response
                switch (response.Status)
                {
                    case AuthenticationStatus.Authenticated:
                        MembershipUser user = MembershipService.GetUser(response);
                        if (user != null)
                        {
                            var authCookie = FormsService.SignInEmailCookie(user.UserName, user.Email, false);
                            Response.Cookies.Add(authCookie);
                        }
                        else
                        {
                            var fetch = response.GetExtension<FetchResponse>();
                            string email = null;
                            if (fetch != null)
                            {
                                email = fetch.GetAttributeValue(WellKnownAttributes.Contact.Email);
                            }

                            string password = RandomStringGenerator.GenerateRandomString(64);
                            var model = new RegisterModel()
                            {
                                UserName = response.ClaimedIdentifier,
                                Email = email,
                                Password = password,
                                ConfirmPassword = password
                            };

                            // Attempt to register the user
                            MembershipCreateStatus createStatus = MembershipService.CreateUser(model.UserName, model.Password, model.Email);

                            if (createStatus == MembershipCreateStatus.Success)
                            {
                                var authCookie = FormsService.SignInEmailCookie(model.UserName, model.Email, false);
                                Response.Cookies.Add(authCookie);
                                return RedirectToAction("Index", "Home");
                            }
                            else
                            {
                                ModelState.AddModelError("", AccountValidation.ErrorCodeToString(createStatus));
                                return View("LogOn");
                            }
                        }

                        if (!string.IsNullOrEmpty(returnUrl))
                        {
                            return Redirect(returnUrl);
                        }
                        else
                        {
                            return RedirectToAction("Index", "Home");
                        }
                    case AuthenticationStatus.Canceled:
                        //ViewData["Message"] = "Canceled at provider";
                        ModelState.AddModelError("", "Canceled at provider");
                        return View("LogOn");
                    case AuthenticationStatus.Failed:
                        //ViewData["Message"] = response.Exception.Message;
                        ModelState.AddModelError("", response.Exception.Message);
                        return View("LogOn");
                }
            }
            return new EmptyResult();
        }
Пример #2
0
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                MembershipCreateStatus createStatus = MembershipService.CreateUser(model.UserName, model.Password, model.Email);

                if (createStatus == MembershipCreateStatus.Success)
                {
                    var authCookie = FormsService.SignInEmailCookie(model.UserName, model.Email, false);
                    Response.Cookies.Add(authCookie);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("", AccountValidation.ErrorCodeToString(createStatus));
                }
            }

            // If we got this far, something failed, redisplay form
            ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
            return View(model);
        }