如何获取会员中当前用户的userId

How to get userId of current user in membership

我为登录用户设置web.config如下:

<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>

另一方面,用户的属性与具有四个模型的 ViewModel 相关:

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

并且,我将当前用户的 "name" 和 "position" 显示为:

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();
    }

到这个阶段,用户正确登录,"name"和"position"显示为用户信息。 之后,我想获取当前用户的用户 ID。此字段是 "UserInfo".

中的主键

我在另一个控制器中使用此代码(用于另一个视图),但遇到错误:

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

错误是:

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'.

对于引用 System.Web 的网络应用程序,您可以通过 System.Web.HttpContext.Current.User.

获取当前用户

小心,因为这在 multi-threaded 环境中不可靠