public bool ExpireToken(RToken token)
        {
            using (var db = new TokenDbContext())
            {
                db.RTokens.Update(token);

                return(db.SaveChanges() > 0);
            }
        }
Ejemplo n.º 2
0
        public void Add(string token, string EmailId)
        {
            var _token = new Token {
                access_Token = token, Email = EmailId
            };

            _context.TokenDb.Add(_token);
            _context.SaveChanges();
        }
        public bool AddToken(RToken token)
        {
            using (var db = new TokenDbContext())
            {
                db.RTokens.Add(token);

                return(db.SaveChanges() > 0);
            }
        }
        public ActionResult HandleVerify(IssueTokenModel model)
        {
            // Write all your code for sending to the database here; we know we have a valid token and the
            // user has confirmed they want to issue it, so it's time to do that now.
            // send data to the data base  (see the create function below??)
            if (ModelState.IsValid)
            {
                // We will go from a model to an entity because we're saving to the database.
                // We know that the model contains values that are safe to store because of the
                // ModelState.IsValid check above, so we know we won't write anything that'll screw
                // up the database.
                var token = new TokenEntity
                {
                    TokenCode       = Guid.NewGuid(), // Our new identifier. The user doesn't get to pick one, so we do that here.
                    TokenType       = ToEntityTokenType(model.TokenType),
                    TokenContent    = model.TokenContent,
                    IssuingReason   = model.Reason,
                    IssuingComments = model.Comments,
                    EmailAddress    = model.EmailAddress,
                    TokenValue      = model.TokenValue,
                    Status          = true // All issued tokens are by default active - the user can't issue a deactivated token.
                };

                // Now we're adding the token to the database...
                tokens.IssuedTokens.Add(token);
                // ... and saving it.
                tokens.SaveChanges();

                //return View();
                // Assuming we got here, the save worked fine. Go ahead and return control to the page;
                // our Javascript will pop up the alert saying that it's successful.
                return(new HttpStatusCodeResult(HttpStatusCode.OK));
            }
            // Stays on the page if there is an error... (set to display system error message)
            return(View(model));
        }
        public string Build(Credentials creds)
        {
            if (!new DatabaseCredentialsValidator(_DbContext).IsValid(creds))
            {
                throw new AuthenticationException();
            }
            var token = BuildSecureToken(TokenSize);
            var user  = _DbContext.Users.SingleOrDefault(u => u.Username.Equals(creds.User, StringComparison.CurrentCultureIgnoreCase));

            //_DbContext.Tokens.Add(new Token { Text = token, User = user, CraetedDate = DateTime.Now });//change to CreateDate
            _DbContext.Tokens.Add(new Token {
                Text = token, User = user, CraetedDate = DateTime.Now, Expiration = DateTime.Now.AddSeconds(43200)
            });                                                                                                                                         //change to CreateDate
            _DbContext.SaveChanges();
            return(token);
        }
Ejemplo n.º 6
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext <TokenDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("TokenDb")));

            services.AddCors(corsOptions =>
            {
                corsOptions.AddPolicy("fully permissive", configurePolicy => configurePolicy.AllowAnyHeader().AllowAnyMethod().WithOrigins("http://localhost:4200").AllowCredentials()); //localhost:4200 is the default port an angular runs in dev mode with ng serve
            });

            services.AddIdentity <ApplicationUser, IdentityRole>()
            .AddEntityFrameworkStores <IdentityDbContext>()
            .AddDefaultTokenProviders();

            services.AddDbContext <IdentityDbContext>();
            services.AddScoped <DbContext>(sp => sp.GetService <IdentityDbContext>());

            services.AddAuthentication(options =>
            {
                options.DefaultSignOutScheme   = IdentityConstants.ApplicationScheme;
                options.DefaultSignInScheme    = CookieAuthenticationDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = "GitHub";
            })
            .AddCookie()
            .AddGitHub("Github", options =>
            {
                options.ClientId                = "3fa0685e2551a20c0601";
                options.ClientSecret            = "61f60c6c756016aca9b485aba0ab4c81bbe93504";
                options.CallbackPath            = new PathString("/account/HandleExternalLogin");
                options.AuthorizationEndpoint   = "https://github.com/login/oauth/authorize";
                options.TokenEndpoint           = "https://github.com/login/oauth/access_token";
                options.ClaimsIssuer            = "OAuth2-Github";
                options.UserInformationEndpoint = "https://api.github.com/user";
                options.SaveTokens              = true;
                options.ClaimActions.MapJsonKey(ClaimTypes.NameIdentifier, "id");
                options.ClaimActions.MapJsonKey(ClaimTypes.Name, "name");
                options.ClaimActions.MapJsonKey("urn:github:login", "login");
                options.ClaimActions.MapJsonKey("urn:github:url", "html_url");
                options.ClaimActions.MapJsonKey("urn:github:avatar", "avatar_url");
                options.Events = new OAuthEvents
                {
                    OnCreatingTicket = async context =>
                    {
                        var request = new HttpRequestMessage(HttpMethod.Get, context.Options.UserInformationEndpoint);
                        request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                        request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", context.AccessToken);

                        var response = await context.Backchannel.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, context.HttpContext.RequestAborted);
                        response.EnsureSuccessStatusCode();

                        var user = JObject.Parse(await response.Content.ReadAsStringAsync());

                        context.RunClaimActions(user);
                        using (var ctx = new TokenDbContext())
                        {
                            ctx.TokenDb.Add(new Token {
                                Email = "sanjay", access_Token = context.AccessToken
                            });
                            ctx.SaveChanges();
                        }
                    }
                };
            });


            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
        }
Ejemplo n.º 7
0
 public bool AddToken(TokenRepoModel token)
 {
     dbContext.Tokens.Add(token);
     return(dbContext.SaveChanges() > 0);
 }