Adding custom role to user ASP.NET Identity - asp.net

Context of problem
In my web application I've been using ASP.NET Identity. I've created custom user profiles by inheriting the User class from IdentityUser. To be able to connect the Roles with for example rolegroups I've also created a custom role class within my database which inherits from the IdentityRole class.
My database model looks like this:
The problem isn't in creating a user. This just works:
var userManager = new UserManager<User>(new UserStore<User>(_context));
var result = userManager.Create(user, password);
Same with creating a role.
var rm = new RoleManager<Role>(new RoleStore<Role>(_context));
var result = rm.Create(new Role(name));
Problem
But its when I try to attach a role to a user (With the following code)
var um = new UserManager<User>(new UserStore<User>(_context));
var result = um.AddToRole(userId, roleName);
The error I get is basically "The entity type IdentityRole is not part of the model for the current context."
Which is true, btw. But how can I make it that my Role entity is seen as the IdentityRole entity, which it basically is, or has to be.
See the full stacktrace here: http://pastebin.com/sEPv5LiT
Thanks in advance!

As i understand you want to define custom roles for users i prefer to add library project in your solution and manage all membership and roles through this.
Custom Asp.Net Membership
I make library project for that if you want i will provide you

Related

Access custom property from IdentityUser in ValuesController without extension - Web API

I have followed on from the following answer here: How to extend IdentityUser with custom property
My variables I have added (following the above answer) are:
int numberID
string fullName
This is built upon the default Visual Studio 2017 ASP.net Web Application selected with options Web API and Individual User Accounts Authentication.
I am now trying to read the current value of both numberID and fullName of the current user inside the ValuesController of the project.
I have tried doing
var currentUser = System.Web.HttpContext.Current.User.Identity.GetUserId();
var currentnumberID = currentUser.numberID;
But am having no sucess with the last line of code.
I have also used User.Identity.GetUserName() and was wondering if there was a way to access my new variables WITHOUT creating a User Identity Extension?
OR
Better yet, with my Web API app what is the best way to add additional variables / fields for the user. Ie I would like to add both fullName and numberID associated to each user and be able to access these in my Web API controllers (eg call for current user and list the variables associated to the current user). I am beginning to think I have taken the wrong road by trying to use UserIdentity.
EDIT
Ended up biting the bullet and following the very easy approach here: How to Get Custom Property Value of the ApplicationUser in the ASP.Net MVC 5 View?
of adding a User Identity extension. Previous answers seemed complicated but that answer was simple and works!
The problem is that you are reading UserId and treating it like User object.
You need to use UserManager, to get the user User object. Inside controller's action method, you have access to UserManager, so you can simply use it:
var currentUser = UserManager.FindById(User.Identity.GetUserId());
If you are not inside controller's action method, then you need to get the UserManager from HttpContext:
var UserManager = HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();

Asp.Net Identity Custom User Roles (querying)

I have successfully created custom users and roles within the Asp.Net Identity framework, however one section which is lacking is support for the 'Roles' collection within a User. I added a logical 'Deleted' flag within a UserRole and I want this to be included when EF retrieves the info from the database, however all it does is retrieve everything (which isn't what I want). Senario:
User A has a Role of 'admin' (non-deleted)
I then add a Role of 'super-admin' and then delete 'admin'
If I then access my version of IdentityUser (which has the 'Roles' ICollection - custom type) I still see the 2 roles (1 deleted, 1 not)
What I want to happen is on login (using mostly default functions with various sections overridden), when EF retrieves the data it should know to exclude any row which is flagged as deleted, however I have been unable to find any method of doing this. Because all the boilerplate code is locked, I cannot easily see what is happening or find any logical place to override this functionality.
Can anyone help with this?
You could just scan the roles after login and remove them with code similar to this:
var roles = UserManager.GetRoles(userId);
foreach (var roleName in roles)
{
var role = RoleManager.FindByName(roleName);
if (role.IsDeleted) UserManager.RemoveFromRole(userId, roleName);
}

Getting and Displaying AD object properties via a webform

What would be the easiest way to get AD user object properties via a webform based on user input?
To elaborate a bit more this is what I would need:
User enters input to an input field (Employee number - we store it as an extension attribute in AD)
On button click the form returns additional user object properties of the account (such as sAMAccountname, Manager) and displays it on the page (preferably)
I also need to have these properties converted to variables the form can use to pass on to another page sending an e-mail with the retrieved information.
We're using asp for our webforms. So far we only needed to pass user input directly to the mail sender, but this one seems more tricky.
Appreciate any help, thanks!
If you're on .NET 3.5 and up, you should check out the System.DirectoryServices.AccountManagement (S.DS.AM) namespace. Read all about it here:
Managing Directory Security Principals in the .NET Framework 3.5
MSDN docs on System.DirectoryServices.AccountManagement
Basically, you can define a domain context and easily find users and/or groups in AD:
// set up domain context
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
// find a user
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");
if(user != null)
{
// display the various properties of the "user" object in your web page
}
}
The new S.DS.AM makes it really easy to play around with users and groups in AD!

MVC5 - Most Efficient Way to Access the User Object

While learning C# and MVC, I'm using the stock MVC template in VS2013 Preview, I'm trying to get a reference to the User class (what the templates creates in the IdentityModel.cs file) of both the currently logged in user (if applicable) or from a user id guid.
Currently I have this (and it works), but it seems a little convoluted and possibly expensive (I'm not sure how it all works under the hook).
IUser iUser = await Users.Find(User.Identity.GetUserId()); //have to reference Microsoft.AspNet.Identity to access the GetUserId method
IUser iUserFromId = await Users.Find("user id guid placeholder");
User user = iUser != null ? (User)iUser : null;
Is there a cleaner or more efficient way of doing this? Is there away do it without async methods?
The new identity api is intended to be async so you are doing it correctly.
You could write it in one line as:
User user = await Users.Find(User.Identity.GetUserId()) as User;
In RTM it will change a little bit, but the gist of it is the same except the Manager will be have a generic so you don't have to do the cast and will look more like:
User user = await Users.FindAsync(User.Identity.GetUserId())

What's the simplest way to use authentication in ASP.NET with Entity Framework?

I'm new to ASP.NET/Entity Framework, and am trying to get simple signup/login working with EF. Basically, I want to create a new user with custom columns in a table that's accessible from my Entity Framework database context. Something along the lines of:
Auth.CreateUser(Username, Password, new { "Email" = Email, "Realname" = Realname, "JoinDate" = JoinDate });
DbContext db = new DbContext();
var User = db.Users.Where(a => a.Email = "some#email.com").FirstOrDefault();
string Realname = User.Realname;
Should I use SimpleMembership or what with it? And how would I make it accessible from EF? I really want to use one of the built-in membership providers, since the last one I did was a custom one (in PHP) and it got kind of messy.
Thanks!
For the easy way you can use SimpleMembership Provider or
You can use your own custom membership provider that Inherit from
System.Web.Security.MembershipProvider
If you are using SimpleMembership you can get access User data by executing :
db.Users.Find(WebSecurity.CurrentUserId);
UserId in Membership is related to key in Users table, so you can get user data by filtering the key.

Resources