强制 windows 身份验证并允许匿名
Force windows authentication and allow anonymous
我的想法是让我的家人使用他们的 Windows 用户名进入应用程序(这样他们就不必写了),并使用 Identity 将密码和其他内容保存到本地中密度纤维板数据库。问题是我不知道如何强制执行 Windows 身份验证(以便 Context.User.Identity.Name
给我 Windows 用户名)并使用该信息登录身份数据库。为了创建项目,我使用了以个人帐户作为安全类型的 Web 表单模板,并删除了所有 Owin 第三方登录包(Microsoft、Google 等)。
这是我尝试过的:
Default.aspx.cs(我的主页需要认证)
protected void Page_Load(object sender, EventArgs e)
{
//According to Identity comments, ApplicationCookie is the AuthenticationType...
//once logged in through Identity
if (Context.User.Identity.AuthenticationType != "ApplicationCookie")
Response.Redirect("Login.aspx", true);
}
Login.aspx.cs
protected void LogIn(object sender, EventArgs e) //login button handler
{
var manager = Context.GetOwinContext().GetUserManager<UserAdministrator>();
var signinManager = Context.GetOwinContext().GetUserManager<SessionAdministrator>();
string windowsName = Context.User.Identity.Name;
User user = manager.Users.Where(u => u.UserName == windowsName).FirstOrDefault();
// rest of the login code...
}
web.config(全局)
<location path="Login.aspx"> //this should only allow windows logged-in users
<system.web>
<authorization>
<allow users="*" />
<deny users="?" />
</authorization>
</system.web>
</location>
<location path="default.aspx"> // this should only allow Identity-logged in users
<system.web>
<authorization>
<allow users="*" />
<deny users="?" />
</authorization>
</system.web>
</location>
项目属性
- Windows 身份验证设置为启用
- 匿名身份验证设置为禁用
出于某种原因,启动应用程序并浏览到 default.aspx 或 login.aspx,不使用 Windows 身份验证,因此 Context.User.Identity.IsAuthenticated
returns 错误。我怎样才能达到我想要的?
尝试在 default.aspx 节点中移除 <allow users="*" />
它将通过拒绝匿名来强制应用程序使用身份验证
这也算是解决了。
我删除了 Windows 身份验证并切换到表单身份验证,并使用我找到的这段代码获取了 Windows 用户名(不记得是谁用它回答了一个 SO 问题):
System.Security.Principal.WindowsPrincipal windowsUser = new System.Security.Principal.WindowsPrincipal(Request.LogonUserIdentity);
Request.LogonUserIdentity.Impersonate();
string username = windowsUser.Identity.Name.Substring(windowsUser.Identity.Name.LastIndexOf("\") + 1);
我的想法是让我的家人使用他们的 Windows 用户名进入应用程序(这样他们就不必写了),并使用 Identity 将密码和其他内容保存到本地中密度纤维板数据库。问题是我不知道如何强制执行 Windows 身份验证(以便 Context.User.Identity.Name
给我 Windows 用户名)并使用该信息登录身份数据库。为了创建项目,我使用了以个人帐户作为安全类型的 Web 表单模板,并删除了所有 Owin 第三方登录包(Microsoft、Google 等)。
这是我尝试过的:
Default.aspx.cs(我的主页需要认证)
protected void Page_Load(object sender, EventArgs e)
{
//According to Identity comments, ApplicationCookie is the AuthenticationType...
//once logged in through Identity
if (Context.User.Identity.AuthenticationType != "ApplicationCookie")
Response.Redirect("Login.aspx", true);
}
Login.aspx.cs
protected void LogIn(object sender, EventArgs e) //login button handler
{
var manager = Context.GetOwinContext().GetUserManager<UserAdministrator>();
var signinManager = Context.GetOwinContext().GetUserManager<SessionAdministrator>();
string windowsName = Context.User.Identity.Name;
User user = manager.Users.Where(u => u.UserName == windowsName).FirstOrDefault();
// rest of the login code...
}
web.config(全局)
<location path="Login.aspx"> //this should only allow windows logged-in users
<system.web>
<authorization>
<allow users="*" />
<deny users="?" />
</authorization>
</system.web>
</location>
<location path="default.aspx"> // this should only allow Identity-logged in users
<system.web>
<authorization>
<allow users="*" />
<deny users="?" />
</authorization>
</system.web>
</location>
项目属性
- Windows 身份验证设置为启用
- 匿名身份验证设置为禁用
出于某种原因,启动应用程序并浏览到 default.aspx 或 login.aspx,不使用 Windows 身份验证,因此 Context.User.Identity.IsAuthenticated
returns 错误。我怎样才能达到我想要的?
尝试在 default.aspx 节点中移除 <allow users="*" />
它将通过拒绝匿名来强制应用程序使用身份验证
这也算是解决了。
我删除了 Windows 身份验证并切换到表单身份验证,并使用我找到的这段代码获取了 Windows 用户名(不记得是谁用它回答了一个 SO 问题):
System.Security.Principal.WindowsPrincipal windowsUser = new System.Security.Principal.WindowsPrincipal(Request.LogonUserIdentity);
Request.LogonUserIdentity.Impersonate();
string username = windowsUser.Identity.Name.Substring(windowsUser.Identity.Name.LastIndexOf("\") + 1);