My code is fairly simple.
var member = Membership.GetAllUsers();
It throws "NotSupportedExpcetion" when this line is executed. I am trying to retrieve a list of users and populate on the web ui. Thank you.
You might be using the default SimpleMembership provider in MVC4? It does not support GetAllUsers() among many other things. My solution has often been to roll my own.
try this
public ActionResult GetUser()
{
UsersContext oModel = new UsersContext();
return View(oModel.UserProfiles.ToList());
}
get all users which were registered using simpleMemberShipProvider [ mvc 4]
Related
We have a WCF application service layer which acts as DataService for our domain models defined in DB. We are writing new REST web services that will work on top of existing application service. Planning to use MVC Web API.
We want to expose our RESTFul APIs as OData endpoint. But looks like OData is tightly coupled with EnityFramework Data Model and Context.
The problem is we can't use EF dbcontext as we need to create objects by requesting them from backend application service and then map to the data model.
Is there a way to implement OData without DB but application service as data source.
Thanks,
M
Looking though the MSDN article introducing OData, it seems that while the scaffolding in Visual Studio is aimed at using OData with EF, you can still create controllers directly by deriving from EntitySetController:
public class PeopleController : EntitySetController<Person, int>
{
public override IQueryable<Person> Get()
{
// return your own non-EF data source
}
}
As long as you can get an IQueryable, you should be able to knock up an OData controller.
You can! The only problematic action you have to take care of is the get.
There are two ways:
Use ODataQueryOptions. It contains the parameters like skip, orderby, etc.
Sample
public class TestController : ODataController
{
public IEnumerable<TestModel> Get(ODataQueryOptions<TestModel> options)
{
var entities = new List<TestModel>()
{
new TestModel { Id = 1 },
new TestModel { Id = 2 },
new TestModel { Id = 3 },
new TestModel { Id = 4 },
new TestModel { Id = 5 },
new TestModel { Id = 6 }
};
//In this example, we use ApplyTo but you can build an adapter to your application service from the options parameter
return (IEnumerable<TestModel>)options.ApplyTo(entities.AsQueryable());
}
}
Implement your own IQueryable Provider that will query the application service. I didn't try this but I don't see any reason for it to not work.
In MVC4 I was using the code
Roles.AddUserToRole(User.Identity.Name, "Approved");
Is there any other way of doing the same(adding user to role "Approved") in MVC5 Identity Model?
EDIT: I meant to ask is this the right way of adding a user to a role? Because in a lot of examples they do not use this code.
You can call AddToRole or AddToRoleAsync as an instance method on any object of type UserManager<TUser> to achieve it in MVC 5, like below:
var _context = new ApplicationDbContext();
var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(_context));
UserManager.AddToRole("UserName", "UserRole");
For more details, take a look at the following link:
http://msdn.microsoft.com/en-us/library/dn468199(v=vs.111).aspx
http://blogs.msdn.com/b/webdev/archive/2013/10/20/building-a-simple-todo-application-with-asp-net-identity-and-associating-users-with-todoes.aspx
Folks,
Envrionment: ASP.NET MVC 4, Razor
I am using SimpleMembership provider for my web application. When the user requests for registration, I need to call WebSecurity.CreateUserAndAccount and also update some other tables. The whole operation has to be transactional. Here is what I am thinking:
using (UsersContext ctx = new UsersContext()) {
using (TransactionScope scope = new TransactionScope()) {
// The following call creates its own context but will call ctx.SaveChanges() internally
WebSecurity.CreateUserAndAccount(...);
// update some other tables using ctx
...
ctx.SaveChanges();
scope.complete();
}
}
I have a feeling this should work. However, I would like to get your expert opinion on whether there is a better way.
Thank you in advance for your help.
Regards,
Peter
TransactionScope is the best way to make it all happen inside the same transaction, but be careful with how your connection strings are defined (and test on Azure if that's where you are deploying it to) as the DTC may get involved and cause you problems in some scenarios (example)
The alternative would be to inherit from SimpleMembershipProvider and override CreateUserAndAccount, as that is all that WebSecurity calls. You could then do all the work inside a single context by duplicating and extending the SimpleMembershipProvider code.
We are currently developing an application based on NHibernate and ASP.NET MVC and a SQL Server backend. Since I'm fairly new to NHibernate, I'm tryig to understand best practices.
Our application requires every user to have it's own SQL Server database. These databases all have an identical structure.
Our customers are identified with a customercode, e.g. 1500.
We've come up with a custom connection provider for nHibernate, which we already use in our nServiceBus backend services:
public class DynamicConnectionProvider : DriverConnectionProvider
{
public override IDbConnection GetConnection()
{
IDbConnection conn = Driver.CreateConnection();
try
{
var messageExecutionContext = ServiceLocator.Current.GetInstance<ITTTContextProvider>().CurrentContext;
if (messageExecutionContext.CustomerId == 0)
{
conn.ConnectionString = ConfigurationManager.ConnectionStrings["dev"]
.ConnectionString;
}
else
{
conn.ConnectionString = ConfigurationManager.ConnectionStrings["default"]
.ConnectionString
.FormatWith(messageExecutionContext.CustomerId);
}
conn.Open();
}
catch (Exception)
{
conn.Dispose();
throw;
}
return conn;
}
}
This connection provider checks the customer code in a context object and sets the connectionstring accordingly.
We are planning to provide a HttpContext aware ITTTContextProvider. For this I have two questions:
How can we retrieve the customer code from the url and put it into our context object for every request? when we use the following route?
<main-site-url>/{customercode}/{controller}/{action}/{id}
Is this method of connecting to several identical databases valid or is it better practice to construct a sessionfactory foreach customer database?
In order to get the customercode you need to access the route data, something along the lines of
HttpContextBase currentContext = new HttpContextWrapper(HttpContext.Current); //ServiceLocator.Current.GetInstance<ITTTContextProvider>().CurrentContext;
RouteData routeData = RouteTable.Routes.GetRouteData(currentContext);
var cusomterCode = routeData.Values["customercode"]
My second suggestion would be not to put this code in the above snippet provided. Abstract it away. See Joshua's answer which highlights the approach I am thinking of.
Can't really help on the second question, actually not familiar with both frameworks mentioned.
See my recent blog post which shows how to use a subdomain to connect to different databases, although it would be easy to implement your own version of ITenantContext that grabbed the customer code from the request url. Also uses separate session factories for each tenant.
http://www.yellowfeather.co.uk/2011/01/multi-tenancy-on-sharp-architecture/
I've been told that MVC 1.0 TempData does not work under a load balancer when using SQL Server and that it is because the Dictionary itself is not serializable.
We require this for a project and are looking to be able load balancer effectively.
So I would be very grateful if someone could answer the following questions:
Is there away around this so you can make it work?
Is this fixed in MVC 2.0?
Can we create a ITempDataProvider to fix it?
Or has anyone made a fix to the source code for a project of their own they would like to share?
Cheers,
Jamie
The dictionary itself doesn't need to be serializable. It is what you store inside TempData that needs to be serializable. So for example if you have the following class
[Serializable]
public class Foo
{
public string Bar { get; set; }
}
You can perfectly fine use SQL server for session persistence and write the following code:
TempData["foo"] = new Foo { Bar = "bar" };
Session["foo"] = new Foo { Bar = "bar" };
Mmmm, so any UI model (ASP.Net MVC) would just require the Serializable attribute and that should just work?
How does it work for lists and collection based UI models?