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.ToClaimsIdentity());

                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.ToClaimsIdentity());

            return(await Task.FromResult(new EmptyResult()));
        }
        public virtual Task SignIn(AuthorizeViewModel model)
        {
            var oauthIdentity = new SentinelIdentity(OAuthDefaults.AuthenticationType, this.Authentication.User.Identity);

            this.Authentication.SignOut(OAuthDefaults.AuthenticationType);
            this.Authentication.SignIn(new AuthenticationProperties()
            {
                RedirectUri = model.RedirectUri
            }, oauthIdentity.ToClaimsIdentity());

            return(Task.FromResult <object>(null));
        }