我正在构建一个MVC 5应用程序.我需要根据AD和SQL数据库或Web服务对人员进行身份验证.
要求是,如果一个人登录到公司网络或通过VPN连接,我必须记录它们而不要求凭据.如果用户通过互联网访问网站或者没有AD帐户,我必须使用表单身份验证.
我正在看这篇文章, 但这将与ASP.Net MVC和OWIN一起使用吗?还有其他选择吗?
提前致谢.
小智 6
我现在也在做一些非常相似的事情.我正在为内部和外部用户提供单一登录门户,他们可以使用他们的AD帐户或指定的用户/密码组合登录.
我目前如何实现这一点(注意这仍然是一项正在进行的工作)是以下列方式.我也使用包含SignInManager 的ASP.NET Identity 2.1 alpha(非常酷).
Request.LogonUserIdentity有已知帐户.然后使用该UserManager.FindAsync方法检查它们是否是有效用途.从这里你可以再次挑战它们,为它们提供一个选项,直接以已知用户身份登录或直接登录(你在这里选择).这个过程中的一些代码片段(这些只是一些样本,可以帮助您完成整个解决方案).
使用Request.LoginUserIdentity登录.这可能是您帐户控制器中的一种方法.
public async Task<ActionResult> WindowsLogin(string returnUrl)
{
var loginInfo = GetWindowsLoginInfo();
var user = await _userManager.FindAsync(loginInfo);
if (user != null)
{
await SignInAsync(user, false);
return RedirectTo(returnUrl, "Manage");
}
return RedirectToAction("Login");
}
private UserLoginInfo GetWindowsLoginInfo()
{
if (Request.LogonUserIdentity == null || Request.LogonUserIdentity.User == null)
{
return null;
}
return new UserLoginInfo("Windows", Request.LogonUserIdentity.User.ToString());
}
Run Code Online (Sandbox Code Playgroud)
我还在ApplicationSignInManager中添加了一个方法(继承自SignInManager),允许用户使用标准登录表单登录其AD详细信息.
public async Task<SignInStatus> WindowsLoginAsync(string userName, string password, bool isPersistent)
{
var signInStatus = SignInStatus.Failure;
using (var context = new PrincipalContext(ContextType.Domain, "YourDomain"))
{
// validate the credentials
bool credentialsValid = context.ValidateCredentials(userName, password);
if (credentialsValid)
{
UserPrincipal userPrincipal = UserPrincipal.FindByIdentity(context, userName);
if (userPrincipal != null)
{
var loginInfo = new ExternalLoginInfo
{
Login = new UserLoginInfo(AuthenticationTypes.Windows, userPrincipal.Sid.ToString())
};
signInStatus = await ExternalSignInAsync(loginInfo, isPersistent);
}
}
}
return signInStatus;
}
Run Code Online (Sandbox Code Playgroud)
然后,这可以在您的Login方法中使用.
Regex domainRegex = new Regex("(domain\\.+)|(.+@domain)");
if (domainRegex.IsMatch(model.Username))
{
result = await _signInManager.WindowsLoginAsync(model.Username, model.Password, model.RememberMe);
switch (result)
{
case SignInStatus.Success:
return RedirectTo(returnUrl, "Manage");
}
}
result = await _signInManager.PasswordSignInAsync(model.Username, model.Password, model.RememberMe, true);
...
Run Code Online (Sandbox Code Playgroud)
我希望其中一些可以帮助您解决问题!
owin 的工作方式是,每个请求都会通过启动时注册的所有中间件模块。
这意味着如果您想要多种方式进行身份验证,您将需要使用/创建和注册所需的所有不同中间件。然后,每个中间件将针对各种用户存储进行身份验证,并创建一个(或多个)ClaimsPrincipal。
一个简单的例子(面向 api)如下所示。OAuthBearer 是来自 Identity 2.0 的令牌身份验证,而 BasicAuthenication 只是标头中的基本用户/密码。
//This will create the usermanager per request.
app.CreatePerOwinContext(ApplicationSession.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
// Token Authentication
app.UseOAuthBearerAuthentication(OAuthBearerOptions);
// Basic Authentication.
app.UseBasicAuthentication(app.CreateLogger<BasicAuthenticationMiddleware>(),
"Realm", ValidateUser);
Run Code Online (Sandbox Code Playgroud)
祝你好运
| 归档时间: |
|
| 查看次数: |
3726 次 |
| 最近记录: |