Are you talking about backend users or users of the front end of your website? What authentication system do you have in place? E.g. the OOTB asp.net identity, azure ad/OAuth provider?
Get an instance of the user store
var userStore = new UserStore<ApplicationUser>(new IdentityDbContext(connectionString));
Substituing whatever user you're using if you're using any other role than the standard ApplicationUser else just user that then you can access all users via
userStore.Users
If you are using AspNetIdentity, you can use UserManager<TUser>.Users (or ApplicationUserManager<TUser>), which TUser is your implementation of ApplicationUser
Thank you for the suggestions so far. How hard would it be to periodically generate a report in .csv format and have it sent to a location? Or what other suggestions would you have to implement something similar?
It is just another scheduled job, so the "periodically" task is handled for you, other than that it is normal .net code Scheduled jobs | Optimizely Developer Community
Thank you all for the info so far. Some more questions, any recommendations on the best tools for creating a .csv file? Also would you recommend to use the built in CMS E-Mailing capabilities to send this report to someone, or is better/safer to have the file sent/stored at a specific location?
I think you are over thinking it. Check your requirements to make sure you fulfill it, then do a minimal approach. You can always improve later
A search for "C# convert List<T> to csv" would be enough for first question.
Cms has no builtin mail capacity but you can simply write one using .net. Depends on what you need, but you might store the csv as an asset with specific access rights granted to certain people, and send the url to those.
Sorry this is a pretty dumb question, but how do I find out the relevent "connectionString" from our project ?
var userStore = new UserStore<ApplicationUser>(new IdentityDbContext(connectionString));
I am trying to put the following example into a ViewModel:
var userStore = new UserStore<ApplicationUser>(new IdentityDbContext(connectionString));
Like so:
using EPiServer.Cms.UI.AspNetIdentity;
using Microsoft.AspNet.Identity.EntityFramework;
namespace WebProject.Site.Models.ViewModels
{
public class UsersReportViewModel
{
var userStore = new UserStore<ApplicationUser>(new IdentityDbContext("EPiServerDB"));
}
}
The error I am getting is:
The contextual keyword 'var' may only appear within a local variable declaration or in script code
What type would you recommend here? I would like to be able to access it from my view so that I can create a table listing the users.
just replace var with UserStore<ApplicationUser> ?
public class UsersReportViewModel
{
public UserStore<ApplicationUser> UserStore {get;set;} = new UserStore<ApplicationUser>(new IdentityDbContext("EPiServerDB"));
}
Should work
Then I must be misunderstanding something crucial here. Don't I need to make the call to the userstore in the Model so that this data is provided to my view? Are there any examples that you could point to that I could look at?
A few things to point out
Thanks guys for the feedback. No scheduled job required anymore (sorry for not mentioning it). I would like to create a custom report in the report center that displays all users (their username, email and whether their account is active or not).
I have managed to create a placeholder page in the report center called Users. Now I am trying to figure what's needed to pull all the users and the required data and display it in a simple table in the Users report.
I have my UsersReportController.cs like this currently:
using System.Web.Mvc;
namespace WebProject.Site.Controllers
{
[EPiServer.PlugIn.GuiPlugIn(
Area = EPiServer.PlugIn.PlugInArea.ReportMenu,
Url = "~/usersreport",
Category = "Users",
DisplayName = "Users")]
[Authorize(Roles = "Administrators, WebAdmins")]
public class UsersReportController : Controller
{
public ActionResult Index()
{
return View();
}
}
}
Is this where I would build the query to get all the users?
I've also got my ViewModel called UsersReportViewModel.cs where I would like to build the table, currently pretty empty:
using EPiServer.Cms.UI.AspNetIdentity;
using EPiServer.Core;
using EPiServer.DataAbstraction;
using Microsoft.AspNet.Identity.EntityFramework;
using System.Collections.Generic;
namespace WebProject.Site.Models.ViewModels
{
public class UsersReportViewModel
{
}
}
Is it possible to create a periodic (scheduled job) report of all EpiServer users that can be sent to someone? Preferabbly in .csv format?
I would really appreciate any pointing in the right direction or any code examples that I can look into.
How would you recommend to best go about this?