使用服务器 windows 用户验证的 MVC 登录屏幕

MVC login screen that validates with the servers windows users

所以这是我的困境, 我想使用 windows 身份验证,但不是传统意义上的。 我需要用户能够为我提供预先确定的用户的密码。它存在于我的 IIS 运行ning 所在的服务器上。 该用户是该机器上的本地用户。

将 MVC 设置为 windows 身份验证会弹出非常糟糕的弹出窗口 window,您必须在其中登录。我想以我的 Web 应用程序的风格向他们展示一个漂亮的登录 window。

所以我的解决方案是使用个人帐户身份验证。 现在这也很好,但它使用 EF 和数据库,我不想保存和维护任何密码。

所以我找到了这段代码:

PrincipalContext context = 
new PrincipalContext(ContextType.Machine, null);
return context.ValidateCredentials(username, password);

哪个是我想要的,但是。我如何在用户导航到另一个页面后检查他们是否真的通过了身份验证?

我已将个人用户帐户用于另一个网站,您可以使用 [授权] 作为执行此操作的一种方式。但我这里没有实际模型可以验证。

有人知道我该如何解决这个问题吗? 或者有人 运行 遇到类似情况?

我也知道这里没有很多代码,但老实说,我无法真正向你们展示一些值得发布的东西。

编辑: Session 是正确的方法吗?我可以为登录状态设置一个会话变量并在每个页面上检查吗?不过,我更喜欢另一种方法。 (饼干是不行的)

会话应该是要走的路。 另外,您是否考虑过使用外部身份验证?这样你就不需要维护密码了。 http://www.asp.net/web-pages/overview/security/enabling-login-from-external-sites-in-an-aspnet-web-pages-site

您很可能需要使用这个人:https://msdn.microsoft.com/en-us/library/system.web.security.formsauthentication(v=vs.110).aspx 另一个 link: http://www.codeproject.com/Articles/578374/AplusBeginner-splusTutorialplusonplusCustomplusF

另外你可以看看自动生成的AccountController里的IAuthenticationManager,里面有几个有趣的方法,看看你能不能重用它们。

//
        // Summary:
        //     Add information to the response environment that will cause the appropriate
        //     authentication middleware to grant a claims-based identity to the recipient
        //     of the response. The exact mechanism of this may vary.  Examples include
        //     setting a cookie, to adding a fragment on the redirect url, or producing
        //     an OAuth2 access code or token response.
        //
        // Parameters:
        //   identities:
        //     Determines which claims are granted to the signed in user. The ClaimsIdentity.AuthenticationType
        //     property is compared to the middleware's Options.AuthenticationType value
        //     to determine which claims are granted by which middleware. The recommended
        //     use is to have a single ClaimsIdentity which has the AuthenticationType matching
        //     a specific middleware.
        void SignIn(params ClaimsIdentity[] identities);
        //
        // Summary:
        //     Add information to the response environment that will cause the appropriate
        //     authentication middleware to grant a claims-based identity to the recipient
        //     of the response. The exact mechanism of this may vary.  Examples include
        //     setting a cookie, to adding a fragment on the redirect url, or producing
        //     an OAuth2 access code or token response.
        //
        // Parameters:
        //   properties:
        //     Contains additional properties the middleware are expected to persist along
        //     with the claims. These values will be returned as the AuthenticateResult.properties
        //     collection when AuthenticateAsync is called on subsequent requests.
        //
        //   identities:
        //     Determines which claims are granted to the signed in user. The ClaimsIdentity.AuthenticationType
        //     property is compared to the middleware's Options.AuthenticationType value
        //     to determine which claims are granted by which middleware. The recommended
        //     use is to have a single ClaimsIdentity which has the AuthenticationType matching
        //     a specific middleware.
        void SignIn(AuthenticationProperties properties, params ClaimsIdentity[] identities);

用法可能是这样的:

var userIdentity = await CreateUserIdentityAsync(user).WithCurrentCulture();

 AuthenticationManager.SignIn(
                new AuthenticationProperties { IsPersistent = isPersistent }, 
                userIdentity);