示例#1
0
        public async Task <ActionResult> ExternalLoginCallback(string returnUrl)
        {
            if (Request.IsAuthenticated)
            {
                return(RedirectToManageLoginsPage(UnexpectedExtrnalLoginError));
            }

            var loginInfo = await OwinAuthenticationManager.GetExternalLoginInfoAsync();

            if (loginInfo == null)
            {
                return(RedirectToAction("Login", new { error = "external-login-failure" }));
            }

            var user = await OwinUserManager.FindAsync(loginInfo.Login);

            if (user != null)
            {
                // The user has an acoount. Sign her in.
                await new LoginHelper(OwinUserManager, OwinAuthenticationManager).Sigin(user, true);
                return(RedirectToLocal(returnUrl));
            }
            else
            {
                // If the user does not have an account, then prompt the user to create an account.
                ViewBag.LoginProvider = loginInfo.Login.LoginProvider;
                ViewBag.ReturnUrl     = returnUrl;
                var model = new ExternalLoginBindingModel
                {
                    Email = loginInfo.Email,
                    Name  = loginInfo.ExternalIdentity.Name
                };
                return(View("ExternalLoginConfirmation", model));
            }
        }
        public async Task <IHttpActionResult> PostLogin(JObject value)
        {
            var userName   = (string)value["userName"];
            var password   = (string)value["password"];
            var persistent = (bool)value["persistent"];

            ApplicationUser user = await OwinUserManager.FindAsync(userName, password);

            if (user == null)
            {
                return(BadRequest("The email address or password is incorrect."));
            }

            await new LoginHelper(OwinUserManager, OwinAuthenticationManager).Sigin(user, persistent);

            return(StatusCode(HttpStatusCode.NoContent));

            /*
             * Just in case. In the opposite case of token/bearer authentication. How to pass custom values to the OWIN middleware.
             * data: {
             *  grant_type: 'password',
             *  userName: userName,
             *  password: password,
             *  scope: persistent ? 'persistent_cookie' : 'session_cookie', // Pass our custom value. Scope may be a list of values separated by spaces.
             * },
             * public override void GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
             * {
             *  // AF. A hack to enable the option of session or persistent cookies. We piggy-back the request and pass our custom value. Scope may be sent as a list of values separated by spaces.
             *  var isPersistent = context.Scope.Any(i => i == "persistent_cookie");
             * }
             */
        }