public async Task <IActionResult> Register(User.Fields fieldList)
        {
            SqlCommand commnd  = new SqlCommand();
            var        dicData = new Dictionary <string, object>();

            dicData[Dictionary_Fields.FullName]      = fieldList.FullName;
            dicData[Dictionary_Fields.Email]         = fieldList.Email;
            dicData[Dictionary_Fields.Passwords]     = fieldList.Passwords;
            dicData[Dictionary_Fields.StatementType] = Dictionary_Fields.Register;
            await hlp.Operations(commnd, Pro, dicData);

            SqlDataReader reader = await commnd.ExecuteReaderAsync();

            if (reader.HasRows)
            {
                while (reader.Read())
                {
                    sqlMessage.Code    = reader.GetString(0);
                    sqlMessage.Message = StringExtensions.Msg(reader.GetString(1));
                }
            }
            else
            {
                Console.WriteLine("رکوردی یافت نشد");
            }
            reader.Close();
            return(new JsonResult(sqlMessage));
        }
        public string Generate(User.Fields user)
        {
            var secretKey          = Encoding.UTF8.GetBytes(_appSettings.Secret); // longer that 16 character
            var signingCredentials = new SigningCredentials(new SymmetricSecurityKey(secretKey), SecurityAlgorithms.HmacSha256Signature);

            var claims = _getClaims(user);

            var descriptor = new SecurityTokenDescriptor
            {
                Issuer             = _appSettings.Issuer,
                Audience           = _appSettings.Audience,
                IssuedAt           = DateTime.Now,
                Expires            = DateTime.Now.AddHours(2),
                SigningCredentials = signingCredentials,
                Subject            = new ClaimsIdentity(claims)
            };

            //JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
            //JwtSecurityTokenHandler.DefaultMapInboundClaims = false;
            //JwtSecurityTokenHandler.DefaultOutboundClaimTypeMap.Clear();

            var tokenHandler = new JwtSecurityTokenHandler();

            var securityToken = tokenHandler.CreateToken(descriptor);

            var jwt = tokenHandler.WriteToken(securityToken);

            return(jwt);
        }
示例#3
0
        public async Task <IActionResult> Login([FromBody] User.Fields user)
        {
            DataTable dt = hlp.ToDataTable(await tblUser.Login(user));

            //var tokenHandler = new JwtSecurityTokenHandler();
            //var key = Encoding.ASCII.GetBytes(_appSettings.Secret);
            //var claims = new ClaimsIdentity();
            //DataTable dtAccess = hlp.ToDataTable(await tblAccessLevel.Select(user.Email));
            //if (dtAccess.Rows.Count > 0)
            //{
            //    for (int i = 0; i < dtAccess.Rows.Count; i++)
            //    {
            //        ArrayList PermissionList = new ArrayList();
            //        PermissionList.Add(P_AccessLevel.PostShow + dtAccess.Rows[i]["PostShow"].ToString());
            //        PermissionList.Add(P_AccessLevel.PostInsert + dtAccess.Rows[i]["PostInsert"].ToString());
            //        PermissionList.Add(P_AccessLevel.PostUpdate + dtAccess.Rows[i]["PostUpdate"].ToString());
            //        PermissionList.Add(P_AccessLevel.PostDelete + dtAccess.Rows[i]["PostDelete"].ToString());
            //        PermissionList.Add(P_AccessLevel.CourseShow + dtAccess.Rows[i]["CourseShow"].ToString());
            //        PermissionList.Add(P_AccessLevel.CourseInsert + dtAccess.Rows[i]["CourseInsert"].ToString());
            //        PermissionList.Add(P_AccessLevel.CourseUpdate + dtAccess.Rows[i]["CourseUpdate"].ToString());
            //        PermissionList.Add(P_AccessLevel.CourseDelete + dtAccess.Rows[i]["CourseDelete"].ToString());
            //        object[] PermissionObject = PermissionList.ToArray();
            //        foreach (string Permission in PermissionObject)
            //        {
            //            claims.AddClaims(new[]
            //            {
            //                new Claim(Permissions.Permission,Permission),
            //            });
            //        }
            //    }
            //}
            //var tokenDescriptor = new SecurityTokenDescriptor
            //{
            //    Subject = claims,
            //    Expires = DateTime.Now.AddMinutes(5),
            //    SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key),
            //        SecurityAlgorithms.HmacSha256Signature)
            //};
            //var token = tokenHandler.CreateToken(tokenDescriptor);
            var tk = "";

            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    user.FullName = dt.Rows[i][Dictionary_Fields.FullName].ToString();
                    user.Roles    = dt.Rows[i][Dictionary_Fields.Roles].ToString();
                }
                tk = jwtService.Generate(user);
            }
            else if (dt.Rows.Count == 0)
            {
                tk = "";
            }
            return(Content(tk));
        }
        public async Task <IEnumerable <User.Logins> > Login(User.Fields user)
        {
            List <User.Logins> Users  = new List <User.Logins>();
            SqlCommand         commnd = new SqlCommand();
            var dicData = new Dictionary <string, string>();

            dicData[Dictionary_Fields.StatementType] = Dictionary_Fields.Login;
            dicData[Dictionary_Fields.Email]         = user.Email;
            dicData[Dictionary_Fields.Passwords]     = user.Passwords;
            Users = await hlp.DataReaderMapToList <User.Logins>(commnd, Pro, dicData);

            return(Users);
        }
        private IEnumerable <Claim> _getClaims(User.Fields user)
        {
            //JwtRegisteredClaimNames.Sub
            // var securityStampClaimType = new ClaimsIdentityOptions().SecurityStampClaimType;

            var list = new List <Claim>
            {
                new Claim(ClaimTypes.Name, user.FullName),
                new Claim(ClaimTypes.Role, user.Roles)
            };

            return(list);
        }
示例#6
0
 public async Task <IActionResult> Register([FromBody] User.Fields Insert)
 {
     return(new JsonResult(await tblUser.Register(Insert)));
 }