示例#1
0
        public async Task <ActionResult> Index(string id, LoginCredential model)
        {
            var context       = Request.GetOwinContext();
            var env           = Request.GetOwinContext().Environment;
            var signInMessage = env.GetSignInMessage(id);

            var authenticationContext = new LocalAuthenticationContext
            {
                UserName      = model.Username.Trim(),
                Password      = model.Password.Trim(),
                SignInMessage = signInMessage
            };

            OwinEnvironmentService owin        = new OwinEnvironmentService(env);
            MidasUserService       userService = new MidasUserService(owin);
            await userService.AuthenticateLocalAsync(authenticationContext);

            var authResult = authenticationContext.AuthenticateResult;

            if (authResult == null || (authResult.ErrorMessage != null && authResult.ErrorMessage != string.Empty))
            {
                string errorMessage = null;
                if (authResult != null && authResult.ErrorMessage != string.Empty)
                {
                    errorMessage = authResult.ErrorMessage;
                }
                else
                {
                    errorMessage = "Unable to process you authentication request due an error";
                }
                ModelState.AddModelError("AuthError", errorMessage);
                return(View());
            }
            else
            {
                ClearAuthenticationCookiesForNewSignIn(context, authResult);
                IssueAuthenticationCookie(context, id, authResult, model.RememberMe);

                //var redirectUrl = GetRedirectUrl(context, signInMessage, authResult);
                string redirectUrl;
                if (authResult.IsPartialSignIn)
                {
                    var path = authResult.PartialSignInRedirectPath;
                    if (path.StartsWith("~/"))
                    {
                        path = path.Substring(2);
                        path = context.Environment.GetIdentityServerBaseUrl() + path;
                    }

                    var host = new Uri(context.Environment.GetIdentityServerHost());
                    //return new Uri(host, path);
                    redirectUrl = path;
                }
                else
                {
                    redirectUrl = signInMessage.ReturnUrl;
                }

                return(Redirect(redirectUrl));
            }
        }