I'm need a way to get the properties of a class, as I know this is with reflection, but I don't know how to apply it to Flutter.
This is my class:
class Login {
final String name;
final String email;
final String token;
final String refreshToken;
final String createdAt;
final String expiresAt;
final bool isValid;
Login({this.name, this.email, this.token, this.refreshToken, this.createdAt, this.expiresAt, this.isValid});
}
And I have to do something like getOwnProperties like is done in JS.
I need to get an array of the properties that the class has.
There is no reflection available in Flutter. You can use code generation for example with the reflectable package or custom generation using https://pub.dartlang.org/packages/build
Is this possible to retrieve data from mysql table from one session?
For example I have the Session["username"] that show me the name of the user.
but If I want to know the gender of the user, I can know it through only one session?
For example Session["username"].gender ??
instead of opening another Session for gender?
someone told me to open a class in app_code:
public class user
{
public string username { get; set; }
public string gender { get; set; }
}
But I dont know how to use it with only one session.
If its possible, show me how pls!
I am using Sqlite database in my app with RijndaelManaged Encryption. Encryption works fine. The problem is how do i add encryption/decryption to my object class. Below is a class
public class MTBL_USER
{
[PrimaryKey, AutoIncrement]
public int UserID { get; set; }
public string LoginID { get; set; }
public string Password { get; set; }
}
I would like to add encryption/decryption logic for LoginID and Password in getter and setter. something like
public string LoginID
{
get {
EncryptDecryptController decrypt = new EncryptDecryptController ();
return decrypt.Decrypt(LoginID);
}
set {
EncryptDecryptController encrypt = new EncryptDecryptController ();
LoginID = encrypt.Encrypt (value);
}
}
This won't work. What is the best way to achieve this.
Your code has StackOverflow in it, it happens if you set LoginID from LoginID's set method.
Try adding a private field that stores the data.
private string _encryptedLoginID = null;
public string LoginID{
get{ return (new EncruptDecryptController()).Decrypt(_encryptedLoginID);}
set{ _encryptedLoginID = (new EncruptDecryptController()).Encrypt(value);}
}
Notice that a database/json/xml serializer will still see the decrypted value when they ask for it, so this is probably not going to do the trick for you.
Continue with this code and mark the LoginID property as ignored for your method of serialization; mark it internal if you can. For example:
[DataMemberIgnore]
[JsonIgnore]
[XmlIgnore]
public string LoginID{...}
Then add another property that communicates _encryptedLoginID directly
public string EncryptedLoginID {
get {return _encryptedLoginID;}
set {_encryptedLoginID=value;}
}
You can also rename LoginID to CleartextLoginID and EncryptedLoginID to LoginID if you want to keep things with less attributes.
Keep in mind that "encryption" is a term lightly tossed around without ever mentioning a very important part of crypto-security: key management. If your key is easy to recover from your code or config files this entire exercise is pointless. You'd be surprised how easy it is some times to get through such defenses. If you're only slowing your attacker down by a few hours you might as well just B64-encode :). Make sure that's not the case and that your key is properly protected by whatever the OS has to offer - don't store it in config files or code.
Consider a web application like facebook, that can send realtime notifications to users.
What is the best way, using asp.net SignalR, to keep track of which connection ids belong to which user, even if the user disconnects, or reconnects later ?
Check out the following blog post:
Mapping ASP.NET SignalR Connections to Real Application Users
Briefly, you would be adding the connection ids to user on the OnConnected method and remove that connection on the OnDisconnected method. Keep in mind that an application user can have multiple connections. So, you need to have a one to many relationship between your user and the connection ids. The above linked post explains it in details with a sample.
I did this for an internal app. The way I did it is that when a user connects, I have the server ask the user to register themselves. This way I know that not only a user is connected and their signalR connnectionID, but they can also tell me any other information (like username or whatever).
When they reconnect I ask them to do it again.
SignalR will maintain the same connectionID per client even if they reconnect which is nice. A reconnection is not the same as an initial connection. New connections indicate a new client, but a reconnection is on the same client.
In my app I maintained a seperate threadsafe dictionary that I kept track of which user and which connectionID was doing what. This way I can say "oh, send message to user ABC" and look up their connectionID. Then act on the Hub's clients object in signalR for that connectionID. If you do it this way you can even have the same "user" in mutliple connections. Imagine user "abc" is open in two browser tabs. If you went strictly by connectionID they'd be technically two different users. But, by maintaining some sort of local collection grouping users and connections you can now have multiple connections for the same user.
I should mention that if you do it this way, you should make sure your site handles what happens when it restarts and loses all the connection information. For me, when someone reconnects I ask them to again re-identify themselves. This way I can re-build my local dictionary when the server comes online without worry. It does have more overhead because now you are asking all your clients to send information to you, but depending on your user case this could be staggered or bunched or otherwise distributed to help you handle load.
In general, however you get local information (whether by asking the user to supply it), or by http context session info, you need to track it yourself.
Well I used a different approach, I extended the ApplicationUser class like that:
// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
public class ApplicationUser : IdentityUser
{
//public int ApplicationUserId { get; set; }
//public string Name { get; set; }
//public string Address { get; set; }
//public string City { get; set; }
//public string State { get; set; }
//public string Zip { get; set; }
[Required]
public string Email { get; set; }
[Required]
public override string UserName { get; set; }
[NotMapped]
public string ConnectionId { get; set; }
[NotMapped]
public string ChattingUserConnectionId { get; set; }
//public string HomeTown { get; set; }
//public DateTime? BirthDate { get; set; }
}
And in my hub i'm doing something like that:
public class ChatHub : Hub
{
#region Data Members
private static ApplicationDbContext applicationDbContext = new ApplicationDbContext();
private static UserManager<ApplicationUser> userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(applicationDbContext));
private static List<ApplicationUser> connectedUsers = new List<ApplicationUser>();
And when a user connects to the chat, I get his ApplicationUser object by his user name and add it to the connectedUsers list. When he disconnects, I remove him.
I ran into some random exceptions with EF states and such which made me create the ApplicationDbContext and UserManager each time it is accessed instead of setting it in a static object:
private ApplicationUser GetCurrentUser()
{
ApplicationDbContext applicationDbContext = new ApplicationDbContext();
UserManager<ApplicationUser> userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(applicationDbContext));
var userName = Context.User.Identity.GetUserName();
var user = userManager.FindByName<ApplicationUser>(userName);
return user;
}
Edit:
The hub code has some problems loading child objects of the user. This code which is also used in the asp.net template will work better, ApplicationDBContext is not needed:
private ApplicationUserManager _userManager
{
get
{
return HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
}
var user = _userManager.FindByName<ApplicationUser, string>(userName);
There's a very good article on ASP.NET's SignalR tutorial section.
I've included the introductory paragraph below.
Mapping SignalR Users to Connections
Each client connecting to a hub passes a unique connection id. You can
retrieve this value in the Context.ConnectionId property of the hub
context. If your application needs to map a user to the connection id
and persist that mapping, you can use one of the following:
The User ID Provider (SignalR 2)
In-memory storage, such as a dictionary
SignalR group for each user
Permanent, external storage, such as a database table or Azure table
storage Each of these implementations is shown in this topic. You use
the OnConnected, OnDisconnected, and OnReconnected methods of the Hub
class to track the user connection status.
UserID Provider
If you're using ASP.NET's standard membership provider (IPrincipal.Identity.Name) you can just do the following to access a client based on a user account. If you have your own user system you can create your own provider.
public class MyHub : Hub
{
public void Send(string userId, string message)
{
Clients.User(userId).send(message);
}
}
I am new to GAE and especially for Datastore (JDO)
I have an Object Composition : User object has a reference of Contact object.
I am able to store them in datastore. But the code allows to store multiple objects with same “username” which is defined as primary key.
Here is the code snippet
//User class
#PersistenceCapable (identityType = IdentityType.APPLICATION)
public class User{
#PrimaryKey
#Persistent (valueStrategy = IdGeneratorStrategy.IDENTITY)
String username;
#Persistent
Contact contact;
//getters and setters
}
// Contact class
#PersistenceCapable (identityType = IdentityType.APPLICATION)
public class Contact {
#PrimaryKey
#Persistent (valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key username;
#Persistent
String Phone1;
}
//DAO class
public void register() {
User user = new User();
user.setUserName("abc");
Contact contact=new Contact();
contact.setEmail("abc#gmail.com");
user.setContact(contact);
pm.makePersistent(user);
}
If I call this register method twice (or equivalent to submit a registration form twice with same set of username and email id), the datastore is not complaining about duplicate key Exception.
Since I am creating "username" as my KEY I am expecting to get duplicate key Exception. But why is this not happening?
thanks
Ma
What "same set of username and email" ? You set username to be autogenerated, by JDO, so it generates the value for that field not you. Consequently it is unique. Consequently there is no exception.