[英]How to get userId of current user in membership
I set web.config for login users as below: 我为登录用户设置了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>
On the other hand, The properties of users are related to a ViewModel with four models: 另一方面,用户的属性与具有四个模型的ViewModel相关:
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.
之后,我想获取当前用户的用户ID。 This field is primary key in "UserInfo".
该字段是“ UserInfo”中的主键。
I Use this code in another Controller(for another view), but I encountered with an error: 我在另一个Controller(用于另一个视图)中使用此代码,但是遇到错误:
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.
System.ArgumentException:'尝试初始化System.Data.SqlClient.SqlConnection对象时发生错误。 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'.
nerInnerException:ArgumentException:不支持关键字:“元数据”。
For web apps which do referece System.Web
you could get the current user via System.Web.HttpContext.Current.User
. 对于引用
System.Web
Web应用程序,您可以通过System.Web.HttpContext.Current.User
获取当前用户。
Be careful as this is not reliable in a multi-threaded environment 请注意,因为这在多线程环境中不可靠
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.