예제 #1
0
        protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            if (RequestVerbIsRequiredToparse(request))
            {
//                Write(request);

                if (request.RequestUri.Segments[2].ToLower() != "signup" && request.RequestUri.Segments[2].ToLower() != "signin")
                {
                    IEnumerable <string> values = null;
                    bool headerExists           = request.Headers.TryGetValues("x-session-token", out values);
                    if (headerExists)
                    {
                        var parsedCredentials = ParseAuthorizationHeader(values.First());
                        if (parsedCredentials != null)
                        {
                            UserModel userModel = new UserModel()
                            {
                            };
                            using (VirtualtraineesEntities db = new VirtualtraineesEntities())
                            {
                                Trainee trainee = db.Trainees.Find(parsedCredentials.Id);
                                userModel = new UserModel()
                                {
                                    Id    = trainee.Id,
                                    Name  = trainee.Name,
                                    Email = trainee.Email,
                                    Role  = "Trainee"
                                };
                            }
                            request.Properties["UserModel"] = userModel;
                            var claims = new List <Claim> {
                                new Claim(ClaimTypes.Role, "Trainee")
                            };
                            var principal = new ClaimsPrincipal(new ClaimsIdentity(claims, "UserClaims"));
                            HttpContext.Current.User = Thread.CurrentPrincipal = principal;
                        }
                        else
                        {
                            return(request.CreateResponse(HttpStatusCode.Forbidden, "Unauthenticated."));
                        }
                    }
                    else
                    {
                        return(request.CreateResponse(HttpStatusCode.Forbidden, "Unauthenticated."));
                    }
                }
            }


            //else return request.CreateResponse(HttpStatusCode.Forbidden, "Unauthenticated.");
            return(await base.SendAsync(request, cancellationToken).ContinueWith(task =>
            {
                var response = task.Result;
                if (response.StatusCode == HttpStatusCode.Unauthorized && !response.Headers.Contains(BasicAuthResponseHeader))
                {
                    response.Headers.Add(BasicAuthResponseHeader, BasicAuthResponseHeaderValue);
                }
                return response;
            }));
        }
예제 #2
0
        public async Task <ResponseModel> Post(SignupModel model)
        {
            var context = new VirtualtraineesEntities();
            IAuthenticationService authenticationService = new AuthenticationManager(context);
            ResponseModel          response = authenticationService.Signup(model);

            if (response.IsSuccess)
            {
                var     emailer = new SendGridEmailer();
                Message message = EmailFactory.CreateMessage(new List <string>()
                {
                    model.Name
                },
                                                             new List <string>()
                {
                    model.Email
                }, EmailType.Welcome);
                var sent = await emailer.SendEmailAsync(message, new NetworkCredential("foyzulkarim", "Foysal.karim25"));
            }
            return(response);
        }
예제 #3
0
 public ValidationService(VirtualtraineesEntities db)
 {
     this.Db = db;
 }
예제 #4
0
 public TraineeService(VirtualtraineesEntities Db)
 {
     this.Db = Db;
 }
예제 #5
0
 public ContentService(VirtualtraineesEntities db)
 {
     this.Db           = db;
     validationService = new ValidationService(Db);
 }
예제 #6
0
 public ClassController()
 {
     Db = new VirtualtraineesEntities();
 }