User profile image asp.net mvc identity - asp.net-mvc

I'm creating a web application using MVC 5 and Identity. I have so far created a registration system, but I would like to allow users to upload a profile when they register.
I was wondering if it is possible to implement profile pictures with asp.net identity?

Yes. Assuming you're using the default Entity Framework implementation, you can extend the ApplicationUser in a file called Models/IdentityModels.cs.
You can store the image in the database or elsewhere on the file system.
One way to store it in the database is using a byte array in the model (which I believe maps to varbinary(max))...
public class ApplicationUser : IdentityUser
{
public byte[] Image { get; set; }
}
Details on how to save upload an image and save in the database via EF can be found here...
Entity Framework 5 Code first adding an image
Or you can simply save the uploaded file to the file system or to blob storage, then store the path or URL to the image...
public class ApplicationUser : IdentityUser
{
public string ImagePath { get; set; }
}

Best thing todo is to create a second table (or add a column to the current table), add a file upload feature to the registration form. When registration is successful add the picture to the db with EntityFramework. Create a page to return the picture by the userid so you can include it in a page somewhere.
Something like this http://blogs.msdn.com/b/webdev/archive/2013/10/16/customizing-profile-information-in-asp-net-identity-in-vs-2013-templates.aspx but with a profile picture

Related

Extend ASP .NET Identity with custom List<property> and access it in the View

I am using ASP NET Identity 2.0. I need to extend the identity model with an ApplicationOrganization class (many-to-many with ApplicationUser).
So I created new class, ApplicationOrganization with;
public virtual ICollection<ApplicationUser> Users { get; set; }
public virtual ICollection<ApplicationOrganization> Organizations { get; set;}
to ApplicationUser class to create a many-to-many relationship.
I would like to add some combo (html select tag) with available organizations into _Layout.cshtml View. That's a problem for me. In the View I can accces current UserName, UserId or any other string property using Claims. But I don't know how to acces List of ApplicationOrganization connected to User in the View. I would like to avoid creating some new login Session. And I don't want to hit database in every view call.
No answer. Ok, one way I know now is I can serialize list as a string (xml, json or whatever) and store it as Identity claim. When I need it I can parse it back.

Access a table of a separate database using entity framework

I have an MVC website with its own database and everything is working fine. Now I want to access a table of a database from a different MVC site. I added the connection string in the Web.config and named it OldMvcDB. Then I added a class to access this table:
public class OldSiteDB : DbContext
{
public OldSiteDB() : base("name=OldMvcDB") { }
public DbSet<OldTable> OldTables { get; set; }
}
When I try to access this table, I get the error:
The model backing the 'OldSiteDB' context has changed since the database was created.
This is because the old database has a lot of other tables so the context doesn't match.
How can I access this one table without having to duplicate all the items in my new site?
You should add the following to your class constructor:
Database.SetInitializer<OldSiteDB>(null);
From this SO answer.

Saving multiple images with size limitation and bootstrap

I have built a simple ASP.NET MVC5 project that acts as a classifieds application for my college's students. I got everything figured out and implemented except for the images. I am struggling to find the best approach to store the images.
I came up with the following structure:
Ad represents the advertisement model
adImage represents an image(s) for an ad. AdImage has a foreign Key public int AdId { get; set; } and then public virtual Ad TheAdvertisement { get; set; }
Considerations:
I am using bootstrap
I will not consider support older IE browsers
I have to resize the images prior to save
I will be saving the images on the file system and then store their urls back into my database.
I think I know how to upload then store multiple images. I am struggling with how to implement size-limitation validation to my controller and whether it is something I should consider to implement in my project. What is the best approach when dealing with images in classified website?
To Limit the size of image files, take a look at file size upload limitation in ASP.NET MVC that employs web.config file to meet your purpose.
Another way to Limit file size is writing code something like this:
[HttpPost]
public ActionResult Upload(HttpPostedFileBase file)
{
//you can put your existing save code here
if (file != null && file.ContentLength > 10000)
{
//do whatever you want with the file
}
}
And the page File Upload ASP.NET MVC 3.0 contains good samples for you if you don't know how to write a controller to save files on file system.

MVC 4 Simplemembership own contextclass

I'm trying to implement the simplemembership with my own context class like so:
public class DbInitializer : DbContext
{
public DbSet<Car> Cars { get; set; }
public DbSet<UserProfile> UserProfiles { get; set; }
}
This work fine and I get all the tables I need: Cars , UserProfiles, Webpages.Memberships, Webpages.Roles, WebPages.UserInRoles. But when I move all the model classes to another project and run it I only get Car and UserProfile so I'm wondering what more I have to do to get the rest of the tables?
Take a look at the SimpleSecurity open source project for an example of how to put SimpleMembership in its own context. Additional entities are also added to the project to enhance the security model. This is developed as a class library that can be incorporated into any MVC application.
You did not provide enough information to figure out what went wrong when you moved it to a new project. My guess is that you did not call WebSecurity.InitializeDatabaseConnection. Take a look at this article on customizing SimpleMembership which shows the best way to initialize the database.

ASP.NET MVC RavenDB Save User

I know how to save documents to the RavenDB but i'm trying to look in to how I can save a User object:
public class User
{
public string Username {get;set;}
public string Password {get;set;}
public string Email {get;set;}
}
Sure, I can save an object of User but, what about Password encryption? And is there a "proper" or a different way of saving a User object? Saving and Retrieving a User object is my whole goal.
Any help is highly appreciated.
Password encryption you'd have to implement yourself. See an example here:
https://github.com/synhershko/NSemble/blob/master/NSemble.Web/Modules/Welcome/WelcomeModule.cs
https://github.com/synhershko/NSemble/blob/master/NSemble.Core/Nancy/NSembleUserAuthentication.cs
The User object is not different than any other object in your system.

Resources