private string[] _usersSplit = new string[0]; //mảng người dùng protected override bool IsAuthorized(HttpActionContext actionContext) { try { if (actionContext.Request.Headers.GetValues("jwt") != null) { // get value from header string authenticationToken = Convert.ToString( actionContext.Request.Headers.GetValues("jwt").FirstOrDefault()); //authenticationTokenPersistant // it is saved in some data store // i will compare the authenticationToken sent by client with // authenticationToken persist in database against specific user, and act accordingly rs rsdecode = EncodeDecodeJWT.Decode(authenticationToken); return(rsdecode.r && rsdecode.v != null); } return(false); } catch { return(false); } }
// This method must be thread-safe since it is called by the thread-safe OnCacheAuthorization() method. /* * Phương thức này phải được thread-safe vì nó được gọi bởi các phương thức Authorization cache thread-safe () * cái đích của phương thức này là kiểm tra xem user đó đăng nhập đúng hay ko, có quền hay ko có quề * true: có quền * false: không có quên - > simple that! * */ protected virtual bool AuthorizeCore(HttpContextBase httpContext) { this._userServ = DependencyResolver.Current.GetService <IUserRepository>(); try { if (httpContext == null) { throw new ArgumentNullException("httpContext"); } loginVM logvm = MySsAuthUsers.GetAuth(); if (logvm == null) { var jsonnn = myCookies.Get("auth"); if (!string.IsNullOrWhiteSpace(jsonnn)) { try { rs rsdecode = EncodeDecodeJWT.Decode(jsonnn); if (rsdecode.r && rsdecode.v != null) { JwtLoginModel user_cook = (JwtLoginModel)rsdecode.v; var log = _userServ.GetEntry(user_cook.uid); logvm = new loginVM(log); MySsAuthUsers.setLogin(logvm); } } catch (Exception ex) { // _userServ.SSLogOut(); return(false); } } } if (logvm == null) { return(false); } var user_login = _userServ.SSgetUserLoged(); //Auth2. kiểm tra quền hạn theo username if (_usersSplit.Length > 0 && !_usersSplit.Contains(user_login.Username, StringComparer.OrdinalIgnoreCase)) { return(false); } if (_rolesSplit.Length > 0 && !_rolesSplit.Contains(user_login.RoleId.ToString())) { return(false); } //Auth3. Kiểm tra quền hạn theo access role if (user_login.Username != "admin") { if (_quyensSplit.Length > 0 && !IsInRole(user_login.ne_quyenIntArrStr, _quyensSplit)) { return(false); } } return(true); } catch { return(false); } }