Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure ASP.NET MVC 4 Application with Oracle Database

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.

like image 511
Mark Avatar asked Jul 29 '13 02:07

Mark


2 Answers

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

like image 163
Piotr Stapp Avatar answered Nov 10 '22 11:11

Piotr Stapp


Asp.Net SimpleMembership and Asp.Net Membership are different beasts, I am afraid. Asp.Net MVC4 templates use SimpleMembership (WebSecurity.Login).

See my answer about MySql Membership provider usage in Asp.Net MVC4 here. Simply changing connection string or other things will not help you. You need to find an Oracle Asp.Net SimpleMembership or write your own.

like image 30
Atilla Ozgur Avatar answered Nov 10 '22 13:11

Atilla Ozgur