I have to build website with MVC 4 on top of Oracle database. Also I don't want these webpages_Membership or webpages_OAuthMembership tables. Is it necessary to write my own membership provider and role provider? I found this - it might be good solution, but maybe there is a simplier way?
If you don't want to use the tables created by Simple Membership Provider, from solution explorer go to Filters, InitializeSimpleMembershipAttribute.cs and make sure autoCreateTables is set to false:
WebSecurity.InitializeDatabaseConnection("MyContext", "TableToPointTo",
"UserIdColumn", "UserNameColumn", autoCreateTables: false);
Simple Membership Provider inherits from Extended Membership Provider so if you wanted to create your own custom provider you would do something like this:
public class MyMembershipProvider : ExtendedMembershipProvider
{
// inherited methods
}
If you want to look at the logic in the SimpleMembershipProvider class, you can see the source code here.
Related
I started using Laravel 5 and tried to learn things about contracts but i still have some questions about them.
For example I want to alter the Illuminate\Contracts\Auth\PasswordBroker contract (used in App\Http\Controllers\Auth\PasswordController )
The contract is an Interface but somehow Laravel knows what implementation belongs to that contract..
I want to change the implementation to my custom one.
But what is the correct way of loading my custom PasswordBroker class?
Should i bind my custom class in the AppServiceProvider?
$this->app->bind(
'Illuminate\Contracts\Auth\PasswordBroker',
'App\Services\MyPasswordBroker'
);
Where does Laravel itself bind the default implementations?
It's in 'Illuminate/Foundation/Application.php'
'auth.password' => ['Illuminate\Auth\Passwords\PasswordBroker', 'Illuminate\Contracts\Auth\PasswordBroker'],
Can I just overwrite the default bindings?
According to the official documentations: Yes.
You can easily extend and override this class within your own application by overriding this binding.
But remember to replace the original service provider in the config/app.php
Note that this class extends the HashServiceProvider, not the default ServiceProvider base class. Once you have extended the service provider, swap out the HashServiceProvider in your config/app.php configuration file with the name of your extended provider.
Your case
So, in your case, you need to extend the 'Illuminate\Auth\Passwords\PasswordResetServiceProvider' to remain other things happened in the service provider.
Take a look at the default service provider.
Let me know if you have other questions.
Our MVC3 application is using Fluent NHibernate and requires implementation of user login, authentication and authorization.
I've seen articles using the [Authorize] method in the controller classes. However, I'm not sure how this all works in our situation given that Fluent NHibernate is in use.
Can anyone share some suggestions as to how to make this work?
You could write a custom role provider implementing the RoleProvider class. In your custom implementation you of the RoleProvider class you could use whatever database access technology you want - FluentNhibernate or whatever. Basically you are interested in implementing the IsUserInRole method.
Then decorate your controllers/actions with the Authorize attribute:
[Authorize(Roles = "Admin")]
public ActionResult Foo()
{
return Content("Only administrators can see this message");
}
And here's another blog post that covers writing a custom role provider in more details.
I want to do GetPassword() on MembershipUser but the custom membership provider in the application is throwing NotImplemented exception, let me know some workaround to get any users password ?
Thanks
If you are using a custom membership provider, that means you have created a class that inherits MembershipProvider. When you inherit this class, visual studio will typically go ahead and auto generate the abstract methods that you inherit from MembershipProvider. One of those methods is GetPassword(), and it's up to you to program the logic for it. By default, it throws that exception reminding you that you haven't written the logic for it yet.
If you didn't want to do a custom membership provider, you can use a SqlMembershipProvider instead and let visual studio create the necessary tables and stored procedures to handle everything. A custom membership provider is used when you want full control over the programming logic.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Inject repository to custom membership provider with Ninject
I have searched much about this topic too much the most close answer was here
MVC 3 ninject custom membership context disposed error
but I don't have any idea about the details all I have in my application is a domain contains my entities and abstraction for repositories and the implementation everything works fine when I use my Ninject binding like this
public class NinjectControllerFactory : DefaultControllerFactory{
readonly IKernel _kernel;
public NinjectControllerFactory(){
_kernel=new StandardKernel();
AddBindings();
}
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType){
return controllerType == null
? null
: (IController) _kernel.Get(controllerType);
}
void AddBindings(){
_kernel.Bind<IŁSomeRepository>()
.To<EFSomeRepository>();
but I have no idea how to bind the customer membership provider I have read about this that I have to inject via a poperty but I don't know how, any ideas ?
First, you should be using Ninject.MVC3 rather than your own controller factory. Ninject.MVC3 takes care of hooking everything up, you just need to provide your mappings in App_Start\NinjectWebCommon.cs
Second, don't bother with using Ninject for Membership, unless you're using a custom membership provider. Even then, it's a lot less of a pain if you don't mix Ninject and Membership. I would suggest not bothering with it unless you really know what you're doing.
The problem is that Membership is a static class, that creates a static instance of the Membership Provider. This means it doesn't get destroyed at the end of the request. There are ways around this, but in general, it's just a lot easier to use Membership as-is than try to make it work with DI.
The question you linked to solves a specific problem, relating to injecting business logic into your custom membership provider. If you need to do this, then it might be a good choice. However, I find that most custom membership providers tend to be very simple.
Dear All,
I am using the membership provider of MVC framework, Now i want to implement the Role and Right on My project, All the Role and Right is available on database so how can i implement the Role and Right? is there is any built in function which can i use? also i am using the Ado .net Data Entity Framework..
If I'm understanding what you want to do correctly, you have to annotate your Controller class or ActionResult with the Authorize attribute like this:
[Authorize(Roles="Domain Admins", Users="testuser")]
public class TestController : Controller {
}
Then as long as your membership provider is setup you should be good to go.
It may be worth mentioning that you can always check if a user is in a role with the following code.
User.IsInRole("Domain Admins");
If your using MVC2 then the default project template makes it easy. You should check out the AccountController and AccountModels in a default MVC2 template.
It sounds like you need a custom role provider:
http://davidhayden.com/blog/dave/archive/2007/10/17/CreateCustomRoleProviderASPNETRolePermissionsSecurity.aspx
http://msdn.microsoft.com/en-us/library/8fw7xh74.aspx
http://www.codeproject.com/KB/aspnet/WSSecurityProvider.aspx