验证服务器windows用户的MVC登录屏幕
本文关键字:MVC 登录 屏幕 用户 服务器 windows 验证 | 更新日期: 2023-09-27 17:50:59
这就是我的困境,我想使用windows身份验证,但不是传统意义上的身份验证。我需要用户能够为我提供一个预先确定的用户密码。它存在于IIS运行的服务器上。该用户是该机器上的本地用户。
将MVC设置为windows身份验证会弹出一个可怕的必须登录的弹出窗口。我想给他们展示一个我的Webapp风格的漂亮的登录窗口。
所以我的解决方案是,使用个人帐户认证。现在,这也很好,很漂亮,但它使用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另一个链接: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);