コード例 #1
0
        public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (!ModelState.IsValid)
            {
                return View(model);
            }

            // This doesn't count login failures towards account lockout
            // To enable password failures to trigger account lockout, change to shouldLockout: true
            var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
            switch (result)
            {
                case SignInStatus.Success:
                    Token token = this.data.Tokens
                                    .All()
                                    .Where(t => t.UserName == model.Email)
                                    .FirstOrDefault();

                    if(token == null)
                    {
                        var newToken = this.GetNewToken(model);

                        this.data.Tokens.Add(newToken);
                        this.data.SaveChanges();
                    }
                    else if (this.IsAccessTokenExpired(token))
                    {
                        token = this.GetNewToken(model);
                        token.ExpireDate = DateTime.Now.AddSeconds(token.ExpiresIn);

                        this.data.Tokens.Update(token);
                        this.data.SaveChanges();
                    }                    

                    return RedirectToLocal(returnUrl);

                case SignInStatus.LockedOut:
                    return View("Lockout");

                case SignInStatus.RequiresVerification:
                    return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });

                case SignInStatus.Failure:

                default:
                    ModelState.AddModelError("", "Invalid login attempt.");
                    return View(model);
            }
        }
コード例 #2
0
        private Token GetNewToken(LoginViewModel model)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(GlobalConstants.ServerUri + "/Token");
            request.Method = "POST";
            request.ContentType = "application/x-www-form-urlencoded";
            string postData = "username="******"&password="******"&grant_type=password";
            request.ContentLength = postData.Length;

            var streamWriter = new StreamWriter(request.GetRequestStream());
            streamWriter.Write(postData);
            streamWriter.Close();

            var response = request.GetResponse();
            var responseBody = new StreamReader(response.GetResponseStream()).ReadToEnd();
            Dictionary<string, object> tokenItems = new JavaScriptSerializer().Deserialize<Dictionary<string, object>>(responseBody);

            Token token = new Token();
            token.AccessToken = tokenItems["access_token"].ToString();
            token.Type = tokenItems["token_type"].ToString();
            token.UserName = tokenItems["userName"].ToString();
            token.ExpiresIn = (int)tokenItems["expires_in"];
            token.ExpireDate = DateTime.Now.AddSeconds(token.ExpiresIn);
            token.Expires = tokenItems[".expires"].ToString();
            token.Issued = tokenItems[".issued"].ToString();

            return token;
        }