Ejemplo n.º 1
0
        public async Task <ActionResult> LoginComplete(string code)
        {
            if (code == null)
            {
                return(RedirectToAction("Index", "Home"));
            }
            SsoAuthProvider provider = GetAuthProvider(StateHashSource.FromRequest);

            string token = await provider.requestAccessTokenByAuthCode(code);

            if (token != null)
            {
                try
                {
                    ActiveDirctoryUser userInfo = await provider.RequestUserData(token);

                    CcRepUser user = await UserManager.FindByNameAsync(userInfo.username);

                    if (user == null)
                    {
                        user = new CcRepUser {
                            UserName = userInfo.username, Email = userInfo.email, FullName = userInfo.name
                        };

                        string userId = await UserManager.CreateWithClaims(user);
                    }

                    ClaimsIdentity claim = await UserManager.CreateIdentityAsync(user,
                                                                                 DefaultAuthenticationTypes.ApplicationCookie);

                    AuthenticationManager.SignOut();
                    AuthenticationManager.SignIn(new AuthenticationProperties
                    {
                        IsPersistent = true
                    }, claim);

                    return(RedirectToAction("Index", "Home"));
                }
                catch (CantGetUserInfo exception)
                {
                    throw new HttpException("Bad access token parameter - " + exception.Message);
                }
                catch (DbEntityValidationException exception)
                {
                    throw new HttpException($"Ошибка! ({exception.Message})");
                    //return Content(ObjectInfo.Print(exception.EntityValidationErrors).ToString());
                }
            }

            throw new HttpException("Wrong parameters!");
        }
Ejemplo n.º 2
0
        //Redirection to SSO ActiveDirectory-auth server
        public ActionResult Login(string returnUrl)
        {
            bool logged = System.Web.HttpContext.Current.User.Identity.IsAuthenticated;

            if (!logged)
            {
                SsoAuthProvider authProvider = GetAuthProvider(StateHashSource.GenerateNew);
                Uri             redirectLink = authProvider.GenerateRedirToAuthUri();
                authProvider.setStateKeyToResponse();

                ViewBag.redirectUrl = redirectLink.ToString();
                return(PartialView("Redir"));
            }

            return(RedirectToAction("Index", "Home"));
        }