public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            ApplicationUser user;
            IdentityRole    userRole;

            context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
            AuthenticationProperties authProperties;

            using (AuthRepository.AuthRepository _repo = new AuthRepository.AuthRepository())
            {
                user = await _repo.FindUser(context.UserName, context.Password);

                if (user == null)
                {
                    context.SetError("invalid_grant", "The user name or password is incorrect.");
                    return;
                }
                else
                {
                    bool isAdmin  = false;
                    var  identity = new ClaimsIdentity(context.Options.AuthenticationType);
                    identity.AddClaim(new Claim("sub", context.UserName));
                    if (user != null)
                    {
                        if (user.Roles.Any())
                        {
                            userRole = await _repo.GetRole(user.Roles.Select(x => x.RoleId).First());

                            if (userRole.Name == "Administrator")
                            {
                                isAdmin = true;
                            }
                            identity.AddClaim(new Claim(ClaimTypes.Role, userRole.Name));
                        }
                    }


                    authProperties = new AuthenticationProperties(new Dictionary <string, string>()
                    {
                        {
                            "Full Name", user.Name
                        },
                        {
                            "User Id", user.Id
                        },
                        {
                            "IsAdmin", isAdmin?"Yes":"No"
                        },
                        {
                            "ProfilePicture", user.ProfilePicture == null ? "NA":user.ProfilePicture
                            //"ProfilePicture","Hello"
                        }
                    });


                    var authTickect = new AuthenticationTicket(identity, authProperties);
                    context.Validated(authTickect);
                }
            }
        }
Ejemplo n.º 2
0
        public async Task <IHttpActionResult> GetInterviewDetails()
        {
            try
            {
                var claimsIdentity = RequestContext.Principal.Identity as ClaimsIdentity;
                var userName       = claimsIdentity.Claims.Where(x => x.Type == "sub").Select(y => y.Value).SingleOrDefault();
                var user           = await _authRepository.FindUser(userName);

                var interviewDetails = await _unitOfWork.GetInterviewRepository().GetInterviewDetails(user.Id);

                if (interviewDetails == null || interviewDetails.Count == 0)
                {
                    return(NotFound());
                }
                return(Ok(interviewDetails));
            }
            catch
            {
                return(InternalServerError());
            }
        }
        public async Task <IHttpActionResult> FindUser(string userName)
        {
            try
            {
                ApplicationUser user = await _repo.FindUser(userName);

                if (user == null)
                {
                    return(NotFound());
                }
                else
                {
                    var OTP = _unitOfWork.GetForgotPasswordRepository().SaveOTP(new Common.DTO.ForgotPasswordDTO()
                    {
                        OTP               = GetOTP(),
                        UserId            = user.Id,
                        TokenCreationDate = DateTime.Now
                    });
                    await _unitOfWork.Save();

                    if (!string.IsNullOrEmpty(user.Email))
                    {
                        SendEmail(OTP, user.Email);
                    }
                    var userIdResponse = new UserIdResponse()
                    {
                        UserId = user.Id
                    };
                    return(Ok(userIdResponse));
                }
            }
            catch (Exception e)
            {
                return(InternalServerError(e));
            }
        }