I'm working on the ASP.NET Identity. By default, the AspNetUsers table comes with a few columns such as: ID, UserName, HashPassword, Email, EmailConfirmed, Phone etc.
By default, the ID column is the Primary Key and UserName has the Unique Constraint.
How do I make other fields such as Email and/or Phone to have the same unique constraint as the UserName?
What I have done so far:
I manage to add the unique constraint to the database manually, however, unlike the UserName field, I was unable to do the validation on the application level. For example: The UserName field will display the error message "Name XXX is already taken" if the user tries to register an account with the same username.
To enforce uniqueness at the UserManager layer, you can implement your own IUserValidator and check for your custom uniqueness rules
Related
Upon database initialization, I want to manually add an initial row to AspNetUsers:
insert AspNetUsers
(UserId,PersonId,UserName,NormalizedUserName,Email,NormalizedEmail,EmailConfirmed,PasswordHash,SecurityStamp,ConcurrencyStamp,PhoneNumber,PhoneNumberConfirmed,TwoFactorEnabled,LockoutEnd,LockoutEnabled,AccessFailedCount) values
(#userIdOut,#personIdOut,#adminUserName,upper(#adminUserName),#AdminEmail,upper(#AdminEmail),#true,null,newid(),null,#AdminPhone,#true,#false,null,#false,0)
However, when I do this, methods like FindByNameAsync don't work.
What I'm doing is:
Setting both NormalizedUserName and NormalizedEmail to upper case
Setting a random GUID for SecurityStamp
I'm handling the initial password on the client side.
What else should I be doing?
I have a table 'user' with the following fields:
id
username
password
salt
folder_files
active
The password is obtained using Rfc2898DeriveBytes. I would like to allow the user to change his password. I read the this guide:
http://www.asp.net/mvc/overview/security/create-an-aspnet-mvc-5-web-app-with-email-confirmation-and-password-reset
I would like to use the GeneratePasswordResetToken. How can I use this method (and therefore also ASP.NET Identity) with che 'user' table (also maintaing the use of Rfc2898DeriveBytes for password hasing)? Can I avoid using the AspNetUsers table?
Unfortunately, that's not possible. However, the AspNetUsers is customizable, as the Password Hasher.
I want to implement an audit table and I have no idea how am I supposed to get the username.
I am using C# and Sql Server. I have a Users table in my database. When I log in my windows form application I verify if the correct combination of username and password is used. But how do I inform the database of the current user? I thought of adding an extra column to my Users table in which to set on 1 the logged username. Is that a solution for single-user? But my application in supposed to support multi-user. What could be done in this case?
Depending on your authentication scheme, you need to get the the User name.
for thick client applications,
Environment.Username
and
System.Security.Principal.WindowsIdentity.GetCurrent()
are a couple of options.
typically for audit tables, there is a column called 'ModifiedByUser' where you can log the user name provided by the win form app.
create the nvarchar and datetime columns (if not already) in your audit table.
one will stored the user name and the other the datetime of the audit action.
in your code, whenever you want to add an entry to the audit table, get Environment.Username or System.Security.Principal.WindowsIdentity.GetCurrent(), along with DateTime.UtcNow and pass it on to be saved to the DB into the Audit table.
SQL Server knows who you are. You can simply use SUSER_SNAME() or/and ORIGINAL_LOGIN() function as a default value for the username column in your audit table. Same for the time of audit event, use GetDate() function. There is no need to send this information from the client.
This is a very open-ended question but I think I understand what you are trying to do. You have application-specfic users that are defined in a Users table (as opposed to using database users or active directory users) and you need to log specific information for auditing purposes or drive security based off of the logins. Is that correct?
This can be done, but the logic for it will need to be written in your application.
Let’s pretend we are writing a program to send out an invoice to a customer.
I used role based security where you can give users access to do specific tasks by granting them a role. For example, “Create New Invoice” could be a role. I usually have 2 tables for this:
SecuirtyRoleDefintion
SecurityRoleUsers
The fist table, Security Role Definition will have an ID column, the Description (“Create New Invoice”), and I usually have a Audit column to indicate if this action needs to be logged for Audit.
The second table, SecurityRoleUsers, is where I define if a user has permission to execute that role. Columns are usually something like this: a unique ID, User ID (foreign key to the Users table), RoleID (foreign key to SecurityRoleDefintion)
Now in your application we need a class to check if a user has a role. It needs to take in the role ID (or name) and the user ID. Example: public bool IsUserAuthorized(int RoleID, int UserID)
This method can run a query on your SecurityRoleUsers table to see if the user is in the table for that role. If so, it returns true. If not, it returns false.
Now back in the application when user click the “Create New Invoice” button it runs the IsUserAuthorized() method to check if a user can perform the operation.
If creating an audit log is necessary, you could do something similar. After the security check is done for “Create New Invoice” you can check to see if the Role needs to be audit logged, if so then write to an Audit table.
DECLARE #username varchar(128)
SET #username = CONVERT(VarChar(128), CONTEXT_INFO());
PRINT #username
DECLARE #ID_User int
SET #ID_User = ( SELECT Users.ID_User
FROM Users
WHERE Users.Username=#username )
PRINT #ID_User
This is how I solved it. I inserted this piece of code in each update trigger.
How would you design a database to manage multi organisation?
(ie 1 user can own/manage more than 1 organisation)
Example
Xero (www.xero.com), you can login to xero then select the company on the list that you want to manage. I think freshbook has something simular.
USER_ACCESS
Id
CompanyUserId (UserId of company)
UserId (UserId that will manages company)
USER_PERMISSION
Id
UserAccessId
CanViewM
CanEditM
....
CanViewN
CanEditN
You should not mix users and logins. They should be kept treated as seperate tables/objects. As ones role in one company might not be the same role as in the other company.
Also do not create a permission table with one column for each possible permission. Instead you should create one row per allowed permission. (and if needed one table defining all permissions)
Hence you should have tables like:
USER_ACCOUNT (used to define logins)
Id
UserName
Password
USER
Id
AccountId (account used for login)
CompanyId (company that the user belongs to)
PERMISSIONS
Id
Name
USER_ALLOWED_PERMISSIONS
UserId
PermissionId
When logging in, simply check the USER table if more than one row is returned for the account and display a select user form if needed.
I am new to SQL and been given a task. Following are the details:
What I have:
A C# desktop application for User login and view user status (only two options: user logs in and check status of all users that are dummy created)
A table named USER containing
id
username
datecreated
A table named LOGINSTAT containing
id
username
Logtime
logDate
What I have to implement
I have to save time and date when ever user logs in in LOGINSTAT table using SQL.
My question
My question is how can I implement that. I can do the coding part but I am interested in getting some good advice to implement it. I think of it as a formal way as I know to do it:
when user logs in insert values into the login table giving all the required values.
BUT
I think that might be a bit odd. Some of my friends said you may be able to implement it by use of foreign key and primary keys, but the problem lies that the user may log in many time in a day. How to keep track of login time and date in that case?
You don't need username in your LOGINSTAT table.
You'll probably want the LOGINSTAT to include:
id
u_id
loginDateTime
id is the unique ID of every login
u_id is a foreign key from the id in users that matches your log event to a user
loginDateTime is a datetime that will give you both your log date and log time in one column
What is unique in LOGINSTAT? Not user by itself, but ID+LogDate+LogTime should be. That would be your primary key.
The only foreign key is in LOGINSTAT: ID, which references the ID in the USER table.
Values in a PRIMARY KEY column (eg. USER.id) must be unique from one another.
Values in a FOREIGN KEY column in another table referencing that primary key (eg. LOGINSTAT.id referencing USER.id) do not need to be unique - you can have multiple records in a table have the same foreign key column reference the same primary key.