Converting unique string to a unique integer - asp.net-mvc

I'm wanting to change our c# asp.net mvc application to work with windows authentication as well as forms authentication (as is currently implemented).
Currently we have a number of tables referencing a user id integer in the user table used by the forms authentication.
Is there an appropriate way of converting the unique string username returned by windows authentication to a unique integer that can be used as the id for the other tables?
An example might be using .GetHashCode() on the username, however I'm not sure if that will definitely create an appropriate integer (ie. unique, always the same integer returned given the same username, etc.)

GetHashCode() changes between framework versions, OS platform, etc., so you cannot rely on it for use as a DB PK.
If you are working with Windows authentication then maintaining the identify is useful for debugging, troubleshooting and possibly impersonation. Why not store it in a table with a Windows Username -> UserID mappings so that you can lookup a User ID given a user name?

Do not use GetHashCode()!
Instead, you should create a database table mapping Windows user accounts to integral user IDs (a Users table).
You can also store additional information about each user in this table.

Related

ASP.NET 2.0 Core MVC Individual User Accounts - why isn't Email field of AspNetUsers unique?

I've built a MVC WebApp using the ASP.NET 2.0 Core MVC template + Individual User Accounts (EF) and notice that the AspNetUsers table in the database created for my WebApp has a non-unique index for Email whereas the UserName index is unique. Further upon account creation UserName is set to the same value as Email, but the user is then allowed to change their Email using the account management page. I am struggling to understand the rationale here.
I would expect each AspNetUser record to relate to a different person. Therefore why not make Email unique? After all, the email confirmation process expects the address to be unique to the user. However, being non-unique means that user might change their email address to one shared by one or more other users e.g. admin#abc.com. Where is the value in such a use case? Indeed, this is behaviour I want to stop on my site.
Questions:
Would changing the AspNetUser Email index to unique have any consequences or otherwise break the framework?
Would allowing the user to change their UserName have any consequences or otherwise break anything? The primary key of AspNetUsers is Id and seems to used as the foreign key by the other identity tables created by the framework.
Can anyone recommend how to check from the account management page (client side) that a new UserName is unique?
I've found other questions on StackOverflow about this sort of thing, but they don't answer the above questions or explain the rationale
asp-net-username-to-email
asp-net-identity-use-email-instead-of-user-name

how to set username not unique in aspnet identity

I am running into a problem in customizing username table in aspNet Identity.
Currently, i am able to login and register with email.After registration,user's Email is filled in email and username column both. now, i know how to seperate these so that username and email both are unique. i have followed this article.http://marcinjuraszek.com/2014/03/asp-net-identity-2-0-0-username-and-email-separation.html
I have already customized the primary key from string to int.
But, now i want to remove unique constraint on username field also.so that two user's with different email id can have same name ?? I am working on a social application where may be at sometime, the user's count could go up to 100k. then its not possible for every user to have its unique name other than email id. please suggest me how to achieve it ?any article or any suggestion or any way to customize it??
The username column is designed to be used as a unique credential for authentication. See it as an alternative to signing-in with an email address. Because of that, you should not try to use it for another task.
It looks like you are trying to use the username column to store the user's name (as in a full name or pseudonym). Again, this field is not designed for that.
You need to use another mechanism to store the user's name. You may create a new field called DisplayName. You can also use a database table to store extra information.
See How to extend available properties of User.Identity and ASP.NET Identity 2.0: Customizing Users and Roles

Ruby on Rails - Implementing UUID as Primary Key With Existing Schema

Currently I am creating a RESTful API for a mobile application. The RESTful API has a number of end points that allow users to exchange personal information between each other. I was testing how secure these endpoints were and quickly realized that if a third party managed to gain access to the API they could easily look up other user's information by guessing their user id or using an automated script to collect a wide range of personal information. This was due to the fact that I was using a primary key that was a simple auto-incremented integer which made it predictable and easy to determine other user's ids. I immediately began looking for something that didn't follow a distinct pattern. I came across UUIDs and decided to implement them with my existing rails app.
Was this a wise decision? I definitely see the upside to using UUIDs but upon further research I found that there were a number of negatives to this approach. Many sources claim that using UUIDs will cause performance issues with large tables. Are UUIDs right for my situation?
My second question is about implementing this in an existing Ruby on Rails application. I made the switch to UUIDs by following this article: http://rny.io/rails/postgresql/2013/07/27/use-uuids-in-rails-4-with-postgresql.html. I ran into an issue with enabling the uuid-ossp extension. I created a migration and put enable_extension 'uuid-ossp' inside the change function. I then changed the existing migrations to support UUIDs as their primary key and ran rake db:drop db:create db:migrate to recreate the database with the edited migrations. This failed with the error PG::UndefinedFunction: ERROR: function uuid_generate_v4() does not exist. I quickly realized that this was because I had created the migration that enabled the uuid-ossp extension after the migrations that I had edited to use UUIDs. When I changed the time stamp in the name of the migration to a date that preceded all migrations the db:migrate command completed with no errors. This felt very hack and defeated the purpose of having migrations. What is the correct way of adding this extension via a migration?
Edit in response to comments:
So a number of comments were made that suggested that I should just be properly authenticating users and checking their permissions before allowing them to view certain data. I have user authentication built into my application but will better explain my situation and why I needed something more than auto-incremented primary keys.
I have a number of users on this application and each user has the ability to create private and public contacts. Public contacts are viewable by everyone using the mobile application. Private contacts can only be viewed by the user who created them. However, a user can share their private contacts with other users by showing other users with the mobile application a QR code that has the contacts ID encoded into it. When the user decodes the contact ID a request is sent to the backend to notify the backend that the user is now an owner of that private contact. This allows the second user to now receive updates from that private contact. This is a large feature of my application. The aim here is to force people to have to exchange these contacts in person and to disallow others from seeing these contacts unless this process has happened.
Implementing this concept proved to be fairly tricky as all users could potentially share all private contacts with any other user on the system. I found this extremely hard to implement using permissions as which contacts a user can view is constantly changing.
Originally I implemented this with auto-incremented integers as my primary key for the contact IDs. It worked but forced me to create a very insecure API endpoint that essentially would take a user ID and a private contact ID as parameters and would add that user as an owner of that contact. Because auto-incremented IDs are so predictable a user with access to the API could essentially loop through a sequence of numbers calling the endpoint each time, pass the sequence number in as the contact ID and add themselves as owners to contacts that hadn't been shared with them. This would by pass the whole process of having to share the contact in person and in large defeats the purpose of having my mobile application.
I decided I needed something less predictable, completely random and unique to each private contact. I found UUIDs while doing research to solve this problem and changed the contact ID in my model to be of type UUID. Are UUIDs the best way to solve this? Should I use something else? Have I gone about solving this problem the wrong way?
Are UUIDs the best way to solve this?
You could use them as a solution. If you do, you should build a new contacts table and model instead of trying to migrate the old model. As well as being tricky to implement, any migration would immediately make existing contact/invite emails invalid (since they contain the old id). Briefly support both models, and retire the old auto-incrementing id model once you are happy that traffic using it is no longer important to your application.
There is still a flaw - your contact share links will now be long-lasting, and if anyone gets access to a contact's id for any reason, and know enough to construct the URL for gaining that user as a contact, then they gain the ability to share it to themselves and anyone else completely outside of the control of your application. This because you are relying on knowledge of the id as the only thing preventing access to the contact details.
Should I use something else?
In my opinion, yes. Use a separate nonce or one-off code model (with UUIDs, or an indexed column containing a long random string - you could use SecureRandom for this) that can grant rights to complete the sharing. When someone wants to share a contact, create the nonce object with details about what is being shared - e.g. the contact_id - and use it to generate email link pointing to a route that will find the nonce and allow access to the resource.
The model doesn't need to be called "Nonce" or contain that as a column, this is just a common name for the pattern. Instead you might call the new model "ContactShare" and the secret property "link_code".
This will allow you to resolve access to contacts using your app's permissions model as normal, and block the possible misuse of sharing links. When the controller with the nonce id or code is invoked, create permissions at that point in order to grant access to the contacts. Then expire or delete the nonce, so it cannot be re-used. I prefer expiry, so you can track usage - this can be as simple as a used boolean column that you update once the sharing request has succeeded.
Note I am not referring to Rack::Auth::Digest nonce routine, which is specific to server authentication. I did not find a RoR pre-built nonce model, but it is possible it goes under a different name.

MVC4: External (OAuth) authentication & mapping to other tables/entities

I am building an MVC4 application and need to use external authentication (Facebook, etc). I have that working fine. I see the SimpleMembershipProvider creates 2 separate tables to handle this: "webpages_Membership" and "webpages_OAuthMembership". I need to map other tables (foreign keys) to User ID. The problem is both tables have a user ID which are unrelated to each other.. I could ignore the foreign keys in the database if necessary (don't like that though), but the bigger question is this: in code, how would I determine whether a user is authorized externally or locally so that I know where to get his other info from? Specifically, I have a user profile table where I will map other attributes to.. having 2 different types of user, how does one go about this?
Right, I have learned more about MVC4 and SimpleMembershipProvider and I see now that the User ID on both tables is actually a foreign key to a parent table called UserProfile. So, User IDs will still be unique.. it's just the mappings are a little different to what I was accustomed to with the standard ASP.NET membership provider. Email address and whatever other properties are required can be easily applied to the UserProfile table.

LINQ to SQL - converting temporary users to real users

In creating a new ASP.NET MVC application, I have an issue with the approach I'm using to store user-created data for temporary users who have yet to create an account which I then try to convert to a real user. That probably doesn't make much sense, so let me explain:
A visitor to the site can enter profile settings before being made to register with a username, password, etc.
I'm creating database entries via LINQ to SQL for a new user in this case, using the Request.AnonymousID value as a temporary username.
When the user chooses to register, I need to 'switch over' the relevant database records to use the newly entered username instead of the temporary one.
The problem is that when I try to update the record I can't because the username is the primary key, so I'm forced to delete the record and add a new one...
I can probably persevere with this, but I think I might just be going about this in completely the wrong way and wondered if anyone could suggest a better way to allow visitors to store information before they've registered and have that carry over when they do.
I know about profiles but want the profile information to be available to other visitors. I also know that I can create an anonymous profile but it seems like I should be able to keep the data model out of the web.config file.
I would suggest having an independent primary key for the table with your custom user data.
And then have fields like RefAnonymousId and RefUserId to relate that user data to the anonymous user and the registered user, respectively.
For example:
TABLE UserData
(
UserDataID int IDENTITY(1,1) PRIMARY KEY,
RefAnonymousId uniqueidentifier,
RefUserId uniqueidentifier,
... (data fields),
(maybe also unique keys on RefUserId and RefAnonymousId)
)
That way you will also be able to identify the user when the user is logged out and maybe automatically log the user in...

Resources