C# User.Identity.Name始终返回我的windows用户名
我正在处理现有的应用程序。我们使用的是ASP.NETMVC4。 当我启动身份验证模式时是Windows,但我想将其更改为表单身份验证。我创建帐户控制器和登录操作,以便从数据库中验证用户。我做了以下几件事 首先,我在web.config文件中将身份验证模式配置为表单C# User.Identity.Name始终返回我的windows用户名,c#,asp.net-mvc-4,authentication,login,C#,Asp.net Mvc 4,Authentication,Login,我正在处理现有的应用程序。我们使用的是ASP.NETMVC4。 当我启动身份验证模式时是Windows,但我想将其更改为表单身份验证。我创建帐户控制器和登录操作,以便从数据库中验证用户。我做了以下几件事 首先,我在web.config文件中将身份验证模式配置为表单 <authentication mode="Forms"> <forms loginUrl="~/Account/Login" timeout="2880" cookieless="UseUri"/> &l
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="2880" cookieless="UseUri"/>
</authentication>**
我的控制器和操作如下所示
[Authorize]
//[初始化SampleMembership]公共类AccountController:控制器 { 实体db=新实体(); 用户=新用户(); // //获取:/Account/ [异名] 公共操作结果登录() { 返回视图(用户); }
// This Action is used to validate weather user is existing or not in a database
[HttpPost]
public ActionResult Login(Users user)
{
//Check Validation
if (ModelState.IsValid)
{
//In this Action we are doing Form Authentication
//Check the entred username and passwords are proper or not
int? flag = db.GET_USER(user.UserName, user.Password).FirstOrDefault();
if (flag == 1)
{
//use Form Authentication class to set cookie
FormsAuthentication.SetAuthCookie(Request.Form["txtUserName"], true);
//@Session["username"] = user.FirstName;
return View("~/Views/Home/Index.cshtml");
}
else
{
ViewBag.ErrorMessage = "The user name or password you entered is incorrect.";
return View("~/Views/Account/Login.cshtml");
}
}
else
{
return View("~/Views/Account/Login.cshtml");
}
}
但是“User.Identity.Name”属性仍然返回我的用户名,而不是经过身份验证的用户的用户名。
有什么我错过的吗
感谢您的帮助甚至不可能同时对Windows和窗体进行身份验证。我会检查您的Web.config并确保身份验证设置与您认为的一致。例如,您的更改可能尚未保存。我实际上看不到您在任何地方缓存用户名。您可以从数据库,但只需设置登录cookie,而不将其存储在任何位置。然后在全局ASP中,您只需拥有
用户
的句柄。这是从哪里来的?@AdmiralAdama我是dot.net的新手。您知道我应该做什么吗。我的意思是,我应该在哪里设置用户配置文件?和“User.Identity”用于表单身份验证?用户登录后如何设置User.Identity.Name属性。@ChrisPratt我保存了所有内容,但对User.Identity.Name属性一无所知。
// This Action is used to validate weather user is existing or not in a database
[HttpPost]
public ActionResult Login(Users user)
{
//Check Validation
if (ModelState.IsValid)
{
//In this Action we are doing Form Authentication
//Check the entred username and passwords are proper or not
int? flag = db.GET_USER(user.UserName, user.Password).FirstOrDefault();
if (flag == 1)
{
//use Form Authentication class to set cookie
FormsAuthentication.SetAuthCookie(Request.Form["txtUserName"], true);
//@Session["username"] = user.FirstName;
return View("~/Views/Home/Index.cshtml");
}
else
{
ViewBag.ErrorMessage = "The user name or password you entered is incorrect.";
return View("~/Views/Account/Login.cshtml");
}
}
else
{
return View("~/Views/Account/Login.cshtml");
}
}