Last month I did a MVC4 project and shared it on codeplex so that it would be easy to get started with MVC4 and MDriven Framework.
But MVC4 is already old. Now it is MVC5 that is what you should use.
So I started to look into that thinking that is was just a matter of changing some assembly references… Well – not really…
The biggest change – for us anyway – is that Microsoft has ditched the provider model for user login etc. Instead they introduced the “ASP.NET Identity”.
So – as the MVC4 sample used the Provider-model-package that has been with us for ages – I know needed to read up on this new thing; ASP.NET Identity.
Luckily it was pretty straight forward. Just implement the following interfaces:
public class UserStoreMDriven:IUserStore<SysUser>,
IUserPasswordStore<SysUser>,
IUserRoleStore<SysUser>,
IUserClaimStore<SysUser>,
IUserLoginStore<SysUser>,
IUserSecurityStampStore<SysUser>
Then make use of the UserStoreMDriven like this:
UserManager = new UserManager<SysUser>(new UserStoreMDriven(this.EcoSpace));
Yellow is things I have implement, Green comes from ASP.NET Identity.
Then like this – pretty much – if not exactly – from MS template for MVC5:
[HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public async Task<ActionResult> Login(LoginViewModel model, string returnUrl) { if (ModelState.IsValid) { var user = await UserManager.FindAsync(model.UserName, model.Password); if (user != null) { await SignInAsync(user, model.RememberMe); return RedirectToLocal(returnUrl); } else { ModelState.AddModelError("", "Invalid username or password."); } } // If we got this far, something failed, redisplay form return View(model); }
The thing I needed to do was this model:
And implement UserStoreMDriven to fulfill all the interfaces that build up the ASP.NET Identity; IUserStore<SysUser>, IUserPasswordStore<SysUser>, IUserRoleStore<SysUser>, IUserClaimStore<SysUser>, IUserLoginStore<SysUser>, IUserSecurityStampStore<SysUser>.
And the class I choose to call SysUser must implement the Microsoft.AspNet.Identity.IUser interface that looks like this:
// Summary: // Minimal interface for a user with a string user key public interface IUser { // Summary: // Unique key for the user // // Returns: // The unique key for the user string Id { get; } string UserName { get; set; } }
To show one example on the implementation of UserStoreMDriven :
System.Threading.Tasks.Task<SysUser> IUserStore<SysUser>.FindByIdAsync(string userId) { var list = (from x in _sp.PSQuery<SysUser>() where (x.Id == userId) select x).ToList(); if (list.Count > 0) return Task.FromResult<SysUser>(list[0]); return Task.FromResult<SysUser>(null); }
This method is used to look up a SysUser from a userId. It formulates a Persistent storage (PS) linq query for all SysUsers that has the correct Id, and it returns the first – in reality it should be at most one that fills the criteria.
Most of the methods in UserStoreMDriven have about the same structure as the one above
You can look it thru in the sample:
You may copy this file or the whole project to use in your MVC5 projects.
The project is availble at CodePlex : https://mdrivenmvc5.codeplex.com/