To use OAuth you need:
  • Uncomment required provider in AuthConfig.cs that located in App_Start
  • Create UserProfile and UserProfileService from here Adding extra information about user
  • Make little changes to standard template code to method "ExternalLoginConfirmation":
        public ActionResult ExternalLoginConfirmation(RegisterExternalLoginModel model, string returnUrl)
        {
            string provider = null;
            string providerUserId = null;

            if (User.Identity.IsAuthenticated || !OAuthWebSecurity.TryDeserializeProviderUserId(model.ExternalLoginData, out provider, out providerUserId))
            {
                return RedirectToAction("Manage");
            }

            if (ModelState.IsValid)
            {
                var userId = WebSecurity.GetUserId(model.UserName);
                if (userId == -1)
                {
                    string str = ConfigurationManager.ConnectionStrings["mongodb"].ConnectionString;
                    DefaultUserProfileService service = new DefaultUserProfileService(str);
                    SampleUserProfile profile = new SampleUserProfile();
                    profile.UserName = model.UserName;
                    service.CreateProfile(profile);

                    OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserName);
                    OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false);

                    return RedirectToLocal(returnUrl);
                }
                else
                {
                    ModelState.AddModelError("UserName", "User name already exists. Please enter a different user name.");
                }

            }

            ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(provider).DisplayName;
            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }
  • Run and enjoy

Last edited May 4, 2013 at 8:18 PM by F0rc0sigan, version 2

Comments

No comments yet.