public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { //context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" }); MTADbContext db = new MTADbContext(); Mapper.CreateMap <AU_NGUOIDUNG, AuNguoiDungVm.CurrentUser>(); AuNguoiDungVm.CurrentUser result = null; var user = db.AU_NGUOIDUNGs.Where(x => x.Username == context.UserName).FirstOrDefault(); if (user != null) { if (user.Password == MD5Encrypt.Encrypt(context.Password)) { result = Mapper.Map <AU_NGUOIDUNG, AuNguoiDungVm.CurrentUser>(user); } } if (user == null) { context.SetError("invalid_grant", "The user name or password is incorrect."); return; } Action <ClaimsIdentity, string> addClaim = (ClaimsIdentity obj, string username) => { return; }; var identity = new ClaimsIdentity(context.Options.AuthenticationType); identity.AddClaim(new Claim(ClaimTypes.Name, context.UserName)); addClaim.Invoke(identity, user.Username); identity.AddClaim(new Claim(ClaimTypes.Role, "MEMBER")); identity.AddClaim(new Claim("unitCode", user.UnitCode)); identity.AddClaim(new Claim("parentUnitCode", user.ParentUnitcode)); AuthenticationProperties properties = new AuthenticationProperties(new Dictionary <string, string> { { "userName", string.IsNullOrEmpty(user.Username)?string.Empty:user.Username }, { "fullName", string.IsNullOrEmpty(user.TenNhanVien)?string.Empty:user.TenNhanVien }, { "code", string.IsNullOrEmpty(user.MaNhanVien)?string.Empty:user.MaNhanVien }, { "phone", string.IsNullOrEmpty(user.SoDienThoai)?string.Empty:user.SoDienThoai }, { "chungMinhThu", string.IsNullOrEmpty(user.ChungMinhThu)?string.Empty:user.ChungMinhThu }, { "unitCode", string.IsNullOrEmpty(user.UnitCode)?string.Empty:user.UnitCode }, { "parentUnitCode", string.IsNullOrEmpty(user.ParentUnitcode)?string.Empty:user.ParentUnitcode } }); AuthenticationTicket ticket = new AuthenticationTicket(identity, properties); context.Validated(ticket); context.Request.Context.Authentication.SignIn(identity); }
public async Task <IHttpActionResult> GetCurrentUser() { var userName = HttpContext.Current.User.Identity.Name; var unitCode = _service.GetCurrentUnitCode(); var currentUser = _service.Repository.DbSet.FirstOrDefault(x => x.Username == userName); var result = new AuNguoiDungVm.CurrentUser() { UserName = userName, MaNhanVien = currentUser.MaNhanVien, TenNhanVien = currentUser.TenNhanVien, SoDienThoai = currentUser.SoDienThoai, ChungMinhThu = currentUser.ChungMinhThu, GioiTinh = currentUser.GioiTinh.ToString(), ChucVu = currentUser.ChucVu, UnitUser = unitCode, }; return(Ok(result)); }
public void WriteLog(AuNguoiDungVm.CurrentUser currentUser) { _servicePeriod.WiteLog(DateTime.Now, currentUser.MaMayBan, currentUser.MaNhanVien, "LOGOUT", currentUser.UnitCode, currentUser.UserName); }