aruni
aruni

Reputation: 2752

How to set user Role in MVC?

I'm using ASP.net MVC. So I want to set the user role when longing.

So this is my controller

public ActionResult LogOn(LogOnModel model, string returnUrl)
{
    if (ModelState.IsValid)
    {                
        var user = GetuserByname(model.UserName);
        if (user.ToList().Count == 1)
        {
            string dbPassword = user.First().UserPassword.ToString();
            string enterPassword = CreatePasswordHash(model.Password, user.First().Salt.ToString());

            if (dbPassword.ToString().Trim() == enterPassword.ToString().Trim())
            {
                FormsAuthentication.SetAuthCookie(user.First().tblUserRole.RoleName, model.RememberMe);
                Session["logged"] = user.First().Username;


                string roleName = user.First().tblUserRole.RoleName;
                Roles.AddUserToRole(model.UserName, roleName);
                return RedirectToAction("Index", "Home");
            }
        }                
    }            
    return View(model);
}

This is my webconfig file

<?xml version="1.0" encoding="utf-8"?>

<configuration>

    <connectionStrings>
        <add name="ApplicationServices1" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />
        <add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />
        <add name="SKGEntities" connectionString="metadata=res://*/Models.Model1.csdl|res://*/Models.Model1.ssdl|res://*/Models.Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=(local);Initial Catalog=SKG;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
    </connectionStrings>

    <authentication mode="Forms">
        <forms loginUrl="~/Login/LogOn" timeout="2880" />
    </authentication>

    <membership>
        <providers>
            <clear />
            <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="3" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
        </providers>
    </membership>

    <profile>
        <providers>
            <clear />
            <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />
        </providers>
    </profile>

    <roleManager enabled="true">
        <providers>
            <clear />
            <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" />
            <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
        </providers>
    </roleManager>

</configuration>

But there is error and it says...

A network-related or instance-specific error occurred while establishing a connection to SQL Server

...I don't know how to fix it.

Also how can I set the...

 Roles.AddUserToRole(xxx, yyy);

...in my controller?

Thank you.

Upvotes: 0

Views: 2091

Answers (1)

Dai
Dai

Reputation: 154995

The error message says your database, a SQL server, is inaccessible.

Your providers (role, membership, profile) are set up to use the ApplicationServices connection string, which is:

data source=.\SQLEXPRESS;
Integrated Security=SSPI;
AttachDBFilename=|DataDirectory|aspnetdb.mdf;
User Instance=true

...however your other connection string is

Data Source=(local);
Initial Catalog=SKG;
Integrated Security=True;
MultipleActiveResultSets=True

...which is a different database server entirely.

Change your provider configuration to use the correct database server.

Upvotes: 3

Related Questions