コード例 #1
0
        public async Task <ActionResult> Login(LoginModel model, string returnUrl)
        {
            try
            {
                var token = await _authenticationEndpoint.GetToken(model);

                var roles = await _authenticationEndpoint.GetUserRoles(token.FullToken);

                AuthenticationProperties options = new AuthenticationProperties();

                options.AllowRefresh = true;
                options.IsPersistent = model.RememberMe;
                if (options.IsPersistent)
                {
                    options.ExpiresUtc = DateTime.UtcNow.AddSeconds(int.Parse(token.Expires_in));
                }

                var claims = new List <Claim>()
                {
                    new Claim(type: ClaimTypes.Name, value: model.EmailAddress),
                    new Claim(type: "AcessToken", value: token.FullToken),
                };

                foreach (string role in roles)
                {
                    claims.Add(new Claim(type: ClaimTypes.Role, value: role));
                }

                var identity = new ClaimsIdentity(claims: claims, authenticationType: "ApplicationCookie");

                Request.GetOwinContext().Authentication.SignIn(properties: options, identities: identity);

                return(RedirectToAction("Dashboard", "Home"));
            }
            catch (BadRequestException ex)
            {
                ModelState.AddModelErrors(ex.Errors);

                return(View(model));
            }
        }