예제 #1
0
        public ActionResult Register(User user, string identifier, string returnUrl)
        {
            //The registration form has been submitted
            try
            {
                if (ModelState.IsValid)
                {
                    OpenId openid = new OpenId
                    {
                        OpenIdUrl = identifier,
                        User = user
                    };

                    users.AddOpenId(openid);
                    users.SaveChanges();

                    // Now let's login out user to out application
                    IssueFormsAuthenticationTicket(user);

                    // We're done, let's get back to where we started from.
                    if (string.IsNullOrEmpty(returnUrl))
                        return RedirectToAction("Index", "Home");
                    else
                        return Redirect(returnUrl);
                }

                var registrationModel = new RegistrationViewModel(identifier)
                {
                    Username = user.Username,
                    Email = user.Email,
                    FullName = user.FullName,
                    ReturnUrl = returnUrl
                };

                return View(registrationModel);
            }
            catch
            {
                var registrationModel = new RegistrationViewModel(identifier)
                {
                    Username = user.Username,
                    Email = user.Email,
                    FullName = user.FullName,
                    ReturnUrl = returnUrl
                };

                return View(registrationModel);
            }
        }
예제 #2
0
        public ActionResult Login(string openid_identifier, string returnUrl)
        {
            var response = openId.GetResponse();

            // If this is the postback from our login form response will be null as we haven't yet dealt with the OpenID provider.
            if (response == null)
            {
                //*************************************************************************************************
                //* Step 1: Here we just display the login form and let the user fill in the OpenID provider URL. *
                //*************************************************************************************************

                // This happens if OpenID identifier isn't set which means that the user didn't fill out the login form yet.
                // All we need to do is display the login form.
                if (string.IsNullOrEmpty(openid_identifier))
                    return View();

                //**********************************************************
                //* Step 2: Send the request to the OpenId provider server *
                //**********************************************************
                return SendRequestToOpenIdProvider(openid_identifier);
            }
            else //If response is not null this is the callback from the OpenID provider.
            {
                //******************************************************
                //* Step 3: OpenId provider sending assertion response *
                //******************************************************

                //This is the point right after the user dealt with the OpenId provider
                switch (response.Status)
                {
                    case AuthenticationStatus.Authenticated:
                        // We first need to get the identifier that the user has logged in with
                        // DotNetOpenAuth gives us a nice serialized identifier so we don't have to deal with
                        // differences between username.myopenid.com and http://www.myopenid.com/gligoran
                        string identifier = response.ClaimedIdentifier;

                        var user = users.GetUser(identifier);
                        if (user != null)
                        {
                            // This user is already registered.
                            // Now we need to let ASP.NET know about the user.
                            IssueFormsAuthenticationTicket(user);

                            // Now let's actually get to where we wanted to go or to the homepage
                            if (string.IsNullOrEmpty(returnUrl))
                                return RedirectToAction("Index", "Home");
                            else
                                return Redirect(returnUrl);
                        }
                        else
                        {
                            // No user with this OpenID was found, so the user will have to register first.
                            // Note that this step can be completely automatic if you get everything you need from OpenID provider.
                            // But as some providers don't return everything you asked for a form for a user to fill out the missing data is a nice addition.
                            var registrationModel = new RegistrationViewModel(identifier)
                            {
                                ReturnUrl = returnUrl
                            };

                            // First let's see which of the requested data we actually got.
                            // We can prefill the registration form with this.
                            var simpleReg = response.GetExtension<ClaimsResponse>();

                            if (simpleReg != null)
                            {
                                if (!string.IsNullOrEmpty(simpleReg.Email))
                                    registrationModel.Email = simpleReg.Email;

                                if (!string.IsNullOrEmpty(simpleReg.FullName))
                                    registrationModel.FullName = simpleReg.FullName;

                                if (!string.IsNullOrEmpty(simpleReg.Nickname))
                                    registrationModel.Username = simpleReg.Nickname;
                            }

                            // Now let's display the registration form
                            return View("Register", registrationModel);
                        }

                    case AuthenticationStatus.Canceled:
                        ModelState.AddModelError("openid_identifier", "Authetication canceled");
                        break;
                    case AuthenticationStatus.Failed:
                        ModelState.AddModelError("openid_identifier", "Authetication failed");
                        break;
                }
            }

            // Something went wrong, so we'll redisplay the Login form which will also hold the error message
            return View();
        }