Configure ASP.NET MVC 4 Application with Oracle Database

Christian Mark picture Christian Mark · Jul 29, 2013 · Viewed 28.2k times · Source

I am currently working on ASP.NET MVC 4 project with Oracle database. I have successfully add the connection string in my Web.config file like here:

<add name="OracleDBConnString" connectionString="Provider=MSDAORA;Data Source=ISDQA;User ID=isd;Password=isdqa;" providerName="System.Data.OleDB" />

But when I create a new project, it has already have a built-in authentication classes. How can I modify these classes once and for all? I want to change the default ConnString.

Here's my model:

public class UsersContext : DbContext
{
    public UsersContext()
        : base("OracleDBConnString")
    {
    }

    public DbSet<UserProfile> UserProfiles { get; set; }
}

public class LoginModel
{
    [Required]
    [Display(Name = "User name")]
    public string UserName { get; set; }

    [Required]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [Display(Name = "Remember me?")]
    public bool RememberMe { get; set; }
}

Here's my User controller:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Login(LoginModel model, string returnUrl)
{
    //I WANT TO MODIFY THIS PART
    if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe)) 
    {

        return RedirectToLocal(returnUrl);
    }

    // If we got this far, something failed, redisplay form
    ModelState.AddModelError("", "The user name or password provided is incorrect.");
    return View(model);
}

UPDATE

When I change the base("DefaultConnection") to base("OracleDBConnString"), I got this error:

Server Error in '/' Application.

A null was returned after calling the 'get_ProviderFactory' method on a store provider instance of type 'System.Data.OleDb.OleDbConnection'. The store provider might not be functioning correctly.

Note that I already added using System.Data.OleDb; on my usings.

Answer

Piotr Stapp picture Piotr Stapp · Jul 31, 2013

Instead of creating special class you can setup in web.config which membership provider you use.

For Oracle there exist an OracleMembershipProvider class, which you can find here. For MySQL you can use MySQLMembershipProvider. There is a nice article how you can setup it: How to Setup and Configure MySql Membership Provider

Moreover you can use Entity Framework Code First Membership Provider and use proper provider (look on this article: Entity Framework Code-First support for Oracle, MySQL, PostgreSQL and SQLite). I did not try this solution, but it looks promising.

If nothing above helps you can write your own Memebership provider, but I am sure that you can find something which already exists.

To sum up: Instead of changing connection type, change membership provider which will be specific for your needs. Your code shouldn't notice change, because every membership provider inherits base MemebershipProivder class