Beispiel #1
0
        public static TokenObjectModel GetGoogleAccesToken(string code)
        {
            string    url = ConfigurationManager.AppSettings["google_auth_url"];
            WebClient wc  = new WebClient();

            wc.QueryString.Add("code", code);
            wc.QueryString.Add("client_id", ConfigurationManager.AppSettings["client_id"]);
            wc.QueryString.Add("client_secret", ConfigurationManager.AppSettings["client_secret"]);
            wc.QueryString.Add("redirect_uri", ConfigurationManager.AppSettings["redirect_uri"]);
            wc.QueryString.Add("grant_type", ConfigurationManager.AppSettings["grant_type"]);


            if (code == null)
            {
                return(null);
            }
            else
            {
                var data                         = wc.UploadValues(url, "POST", wc.QueryString);
                var responseString               = UnicodeEncoding.UTF8.GetString(data);
                GoogleTokenObjectModel token     = JsonConvert.DeserializeObject <GoogleTokenObjectModel>(responseString);
                var              stream          = token.id_token;
                var              handler         = new System.IdentityModel.Tokens.Jwt.JwtSecurityTokenHandler();
                var              jsonToken       = handler.ReadToken(stream);
                var              tokenS          = handler.ReadToken(stream) as JwtSecurityToken;
                string           sub             = tokenS.Claims.First(claim => claim.Type == "sub").Value;
                string           email           = tokenS.Claims.First(claim => claim.Type == "email").Value;
                string           name            = tokenS.Claims.First(claim => claim.Type == "name").Value;
                UserObjectModel  userObj         = new UserObjectModel(name, email, sub);
                TokenObjectModel tokenObj        = new TokenObjectModel();
                bool             isUserExists    = UserHelper.IsUserRegistered(sub);
                bool             registerSuccess = false;
                if (!isUserExists)
                {
                    registerSuccess = UserHelper.RegisterUser(userObj) ? true : false;
                }
                if (isUserExists | registerSuccess)
                {
                    userObj = UserHelper.getUserBySubKey(userObj.Sub);
                    CreateUserSession(userObj);
                    tokenObj = TokenHelper.createToken(userObj);
                    tokenObj = TokenHelper.getTokenByAuthorizationCode(tokenObj.AuthorizationCode);
                    UserTokensObjectModel userTokenObj = new UserTokensObjectModel(userObj.UserId, tokenObj.TokenId);
                    bool success = UserTokensHelper.MapUserToken(userTokenObj);
                }
                return(tokenObj);
            }
        }
Beispiel #2
0
        public HttpResponseMessage getAccessTokenByRefreshToken([FromBody] RefreshTokenObject refreshTokenObj)
        {
            UserObjectModel  userObj  = TokenHelper.getUserByRefreshToken(refreshTokenObj.RefreshToken);
            TokenObjectModel tokenObj = TokenHelper.createToken(userObj);

            tokenObj = TokenHelper.getTokenByAuthorizationCode(tokenObj.AuthorizationCode);
            if (userObj != null || tokenObj != null)
            {
                UserTokensObjectModel userTokenObj = new UserTokensObjectModel(userObj.UserId, tokenObj.TokenId);
                bool             success           = UserTokensHelper.MapUserToken(userTokenObj);
                TokenObjectModel tokenUserObj      = new TokenObjectModel(tokenObj.TokenId, tokenObj.AccessToken, tokenObj.RefreshToken, tokenObj.ExpiresIn, tokenObj.AuthorizationCode, userObj.UserId, userObj.Username, userObj.EmailId);
                var message = Request.CreateResponse(HttpStatusCode.Created, tokenUserObj);
                return(message);
            }
            else
            {
                var message = Request.CreateResponse(HttpStatusCode.Unauthorized, "Invalid Token");
                return(message);
            }
        }
Beispiel #3
0
 public static bool MapUserToken(UserTokensObjectModel userTokenObj)
 {
     try
     {
         SqlConnectionStringBuilder builder = getConnectionString();
         using (SqlConnection connection = new SqlConnection(builder.ConnectionString))
         {
             connection.Open();
             SqlCommand cmd = new SqlCommand();
             cmd.CommandType = System.Data.CommandType.StoredProcedure;
             cmd.Connection  = connection;
             cmd.CommandText = "Proc_UserTokens_CreateUserToken";
             cmd.Parameters.Add(new SqlParameter("@UserId", userTokenObj.UserId));
             cmd.Parameters.Add(new SqlParameter("@TokenId", userTokenObj.TokenId));
             bool IsSuccess = cmd.ExecuteNonQuery() != 0 ? true : false;
             return(IsSuccess);
         }
     }
     catch (SqlException ex)
     {
         Console.WriteLine("Exception:" + ex.Message);
         return(false);
     }
 }