Пример #1
0
        public async Task <ActionResult> Login(CookieLoginViewModel model)
        {
            var user = await this.userManager.AuthenticateUserWithPasswordAsync(model.Username, model.Password);

            if (user.Identity.IsAuthenticated)
            {
                var cookieIdentity = new SentinelIdentity(DefaultAuthenticationTypes.ApplicationCookie, user.Identity);

                this.Authentication.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
                this.Authentication.SignIn(
                    new AuthenticationProperties
                {
                    IsPersistent = true,
                    ExpiresUtc   = DateTimeOffset.UtcNow.AddHours(1),
                    RedirectUri  = model.ReturnUrl
                },
                    cookieIdentity.AsClaimsIdentity());

                if (!string.IsNullOrEmpty(model.ReturnUrl) && Url.IsLocalUrl(model.ReturnUrl))
                {
                    return(this.Redirect(model.ReturnUrl));
                }
            }

            return(this.View(model));
        }
Пример #2
0
        public async Task <ActionResult> AuthorizeClient(OAuthAuthorizeViewModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return(await Task.FromResult(this.View("Authorize", model)));
            }

            // Redirect user back to application with an error message if it rejects
            if (!model.Grant)
            {
                return(await Task.FromResult(this.Redirect($"{model.RedirectUri}?error=access_denied&error_description=User does not grant access&state={model.State}")));
            }

            // Redirect user if it is no longer authenticated
            if (!this.Authentication.User.Identity.IsAuthenticated)
            {
                this.Authentication.Challenge(DefaultAuthenticationTypes.ApplicationCookie);

                return(await Task.FromResult(new HttpUnauthorizedResult()));
            }

            // Log in user with new authentication type
            var identity = new SentinelIdentity(OAuthDefaults.AuthenticationType, this.Authentication.User.Identity);

            this.Authentication.SignOut(OAuthDefaults.AuthenticationType);
            this.Authentication.SignIn(identity.AsClaimsIdentity());

            return(await Task.FromResult(new EmptyResult()));
        }