我是MVC的新手,我需要检索用户名并将其传递给我的公司库,后者检查用户证书。
Web.config
<authentication mode="Windows" />
<authorization>
<allow users="*"/>
<deny users="?"/>
</authorization>控制器
[Authorize]
public class MVCAuthen : Controller
{
public string GetCredentials()
{
var userName = HttpContext.Current.User.Identity.Name;
string credential = library.Getcredential(userName);
return credential;
}
}我的问题是,当我试图检索用户名时,我一直处于空白状态。有人能告诉我我做错了什么或者我如何检索用户名吗?
注意:我试图在本地执行此操作,因为我正在尝试调试它。
发布于 2017-06-23 20:46:40
首先,您应该使用Internet应用程序或Intranet应用程序模板。
然后,在web.config上,您应该注释或删除表单身份验证,并使用windows身份验证。就像这样:
<--
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="2880" />
</authentication>
-->
<authentication mode="Windows" />并将其添加到“appSettings”中
<appSettings>
<add key="autoFormsAuthentication" value="false" />
<add key="enableSimpleMembership" value="false"/>
</appSettings>现在转到解决方案资源管理器,右键单击项目并转到属性。在那里,必须将Windows身份验证更改为“已启用”。
如果不允许任何匿名访问,也可以禁用匿名身份验证。
一旦完成,您就可以在任何Controller或Action上添加授权。
然后,您应该能够使用您的windows密码登录。
如果您能够登录并查看页面,那么您可以像这样检索用户名。
var username = HttpContext.User.Identity.Name;https://stackoverflow.com/questions/44728469
复制相似问题