How to get userId of current user in membership

408 Views Asked by At

I set web.config for login users as below:

<authentication mode="Forms">
  <forms loginUrl="~/Home/Login" defaultUrl="~/" timeout="20" slidingExpiration="true" />
</authentication>
<membership defaultProvider="SqlProvider">
  <providers >
    <clear />
 <add name="SqlProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ParsDataEntities" maxInvalidPasswordAttempts="3" minRequiredPasswordLength="6" passwordAttemptWindow="10" applicationName="/" />
  </providers>
</membership>

On the other hand, The properties of users are related to a ViewModel with four models:

 public class CreateUsersViewModel
{
    public EmployeePosition EmployeePositions { get; set; }
    public Employee Employees { get; set; }
    public UsersInfo UserInfos { get; set; }
    public Department Departments { get; set; }
}

And, I displayed "name" and "position" of current user as:

public ActionResult Login()
    {
        return View();
    }
[HttpPost]
public ActionResult Login(string username, string password, bool remmeberme)
    {
        var db = new ParsDataEntities();
        UsersInfoRepository BlUserInfo = new UsersInfoRepository();
        CreateUsersViewModel UVM = new CreateUsersViewModel();

        if (BlUserInfo.Exist(username, encryptString(password)))
        {
            var ViewModel = (from EP in db.EmployeePositions
                             join Dep in db.Departments on EP.DepID equals Dep.DepID
                             join E in db.Employees on EP.EmpID equals E.EmpID
                             join UI in db.UsersInfos on EP.EmpID equals UI.Usr_EmpID
                             where EP.DepID == Dep.DepID && EP.EmpID == E.EmpID && EP.EmpID == UI.Usr_EmpID
                             select new CreateUsersViewModel
                             {
                                 EmployeePositions = EP,
                                 Departments = Dep,
                                 Employees = E,
                                 UserInfos = UI,
                             }).Where(x => x.UserInfos.Usr_UserName == username).FirstOrDefault();
            var name = ViewModel.Employees.EmpFirstName + ViewModel.Employees.EmpLastName;
            var position = ViewModel.Departments.DepName;

            FormsAuthentication.SetAuthCookie(name+"-"+position, remmeberme);

            return RedirectToAction("/Index");
       }
        else
        {
            ViewBag.Message = "username or password is wrong";
        }
    return View();
    }

Until this stage, the user logged in correctly, and the "name" and "position" are displayed as an users information. after that, I want to get user id of current user. This field is primary key in "UserInfo".

I Use this code in another Controller(for another view), but I encountered with an error:

string currentUserId = Convert.ToString(Membership.GetUser().ProviderUserKey);

the error is:

System.ArgumentException: 'An error occurred while attempting to initialize a System.Data.SqlClient.SqlConnection object. The value that was provided for the connection string may be wrong, or it may contain an invalid syntax.' ّInnerException:ArgumentException: Keyword not supported: 'metadata'.

1

There are 1 best solutions below

0
Cristian E. On BEST ANSWER

For web apps which do referece System.Web you could get the current user via System.Web.HttpContext.Current.User.

Be careful as this is not reliable in a multi-threaded environment