Phi*_*hil 6 asp.net asp.net-mvc windows-authentication
我已经为我的asp.net mvc 1.0 Web应用程序构建了一个自定义登录系统,因为我为每个用户存储了大量用户数据(我决定不再尝试为Windows身份验证添加自定义表).登录系统基本上使用SQL Server(2005或2008)和我自己的数据库和表结构,这是非常标准的.具有唯一ID,用户名和散列密码的用户表,该密码链接到我的其他用户相关数据表.
我的问题是,如何绑定我的系统以使用Windows身份验证登录.我想允许管理员为用户(在我的系统中定义)选择一个Windows身份验证登录,并可能在我的自定义表中添加一个值,我可以使用它来验证它们?
问题可能是错误的,我可能误解了Windows身份验证的工作原理,但我想在我的Web应用程序中提供该选项.
如果您的站点上启用了Windows身份验证,那么您应该能够使用User.Identity.Name获取当前登录用户的NT/Active Directory用户名,并将其与用户表中的列相匹配.
以下是我们为混合表单/ Windows身份验证应用程序完成的工作: -
public class MyBaseController
{
protected override void OnAuthorization( AuthorizationContext authContext )
{
if
(
!User.Identity.IsAuthenticated &&
Request.LogonUserIdentity != null &&
Request.LogonUserIdentity.IsAuthenticated
)
{
String logonUserIdentity = Request.LogonUserIdentity.Name;
if ( !String.IsNullOrEmpty(logonUserIdentity) )
{
User loginUser =
Context.Users.FirstOrDefault(
x => x.UserIdentity == logonUserIdentity);
if ( loginUser != null )
FormsAuthentication.SetAuthCookie(
loginUser.LoginName,createPersistentCookie);
}
}
Run Code Online (Sandbox Code Playgroud)
为了紧凑,我已经采取了一些封装.
归档时间: |
|
查看次数: |
16889 次 |
最近记录: |