我有一个网站,它有自己的登录屏幕,用户在其中输入他们的用户名和密码。点击登录按钮后,用户应该通过 ADFS 进行身份验证,我应该得到一个 SAML 令牌。这行得通。但在这一点上,我不确定我需要做什么才能登录用户。我不确定的原因是因为这个网站有点扭曲。正如我所说,用户通过我们网站的登录页面通过 ADFS 登录。但是,如果用户访问未经授权的页面,而不是将用户重定向到我们的登录页面,我们需要将用户重定向到我们的 ADFS 登录页面。我以这种方式开始了整个项目。用户通过 ADFS 的登录页面进行身份验证。这只需像这样设置我的 ConfigureAuth 方法(在 StartupAuth.cs 中)即可完成:
public void ConfigureAuth(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseWsFederationAuthentication(
new WsFederationAuthenticationOptions
{
Wtrealm = realm,
MetadataAddress = adfsMetadata
});
}
够简单!如果我尝试转到某个[Authorized]
页面,它会将我带到 ADFS 登录页面。在此之后,是时候研究另一种登录方法了(通过使用登录页面使用 ADFS 进行身份验证)。请记住,无论您尝试通过哪种方式进行身份验证,您仍然在使用同一个 ADFS 进行身份验证;一个您正在使用 ADFS 的登录页面,另一个您正在使用自定义登录页面。所以我用一个简单的小表单创建了自定义登录页面,用于登录:
<form action="@Url.Action("SignIn", "Account")" method="post">
<input type="text" name="username" />
<input type="password" name="password" />
<input type="submit" value="Login" />
</form>
这工作得很好。点击提交按钮后,它会尝试登录操作。登录操作如下所示:
[HttpPost]
public ActionResult SignIn(string username, string password)
{
if (!Request.IsAuthenticated)
{
const string _relyingPartyUri = "https://myrelayingparty/";
const string _serverName = "myservername";
const string certSubject = "CN=mycertsubject";
string endpointUri = string.Format("https://{0}/adfs/services/trust/13/usernamemixed", _serverName);
var factory = new WSTrustChannelFactory(new UserNameWSTrustBinding(), endpointUri)
{
TrustVersion = TrustVersion.WSTrust13
};
factory.Credentials.UserName.UserName = @"mydomain\" + username;
factory.Credentials.UserName.Password = password;
var rst = new RequestSecurityToken
{
RequestType = RequestTypes.Issue,
AppliesTo = new EndpointReference(_relyingPartyUri),
KeyType = KeyTypes.Bearer
};
var channel = factory.CreateChannel();
var genericToken = channel.Issue(rst) as GenericXmlSecurityToken;
if (genericToken != null)
{
//Setup the handlers needed to convert the generic token to a SAML Token
var tokenHandlers = new SecurityTokenHandlerCollection(new SecurityTokenHandler[] { new SamlSecurityTokenHandler() });
tokenHandlers.Configuration.AudienceRestriction = new AudienceRestriction();
tokenHandlers.Configuration.AudienceRestriction.AllowedAudienceUris.Add(new Uri(_relyingPartyUri));
var trusted = new TrustedIssuerNameRegistry(certSubject);
tokenHandlers.Configuration.IssuerNameRegistry = trusted;
//convert the generic security token to a saml token
SecurityToken samlToken = tokenHandlers.ReadToken(new XmlTextReader(new StringReader(genericToken.TokenXml.OuterXml)));
//convert the saml token to a claims principal
var claimsPrincipal = new ClaimsPrincipal(tokenHandlers.ValidateToken(samlToken).First());
HttpContext.GetOwinContext().Authentication.SignIn(new AuthenticationProperties { IsPersistent = true, RedirectUri = "Home/Homepage" },
claimsPrincipal.Identities.ElementAt(0));
}
}
return RedirectToAction("Index", "Home");
}
它可以成功检索和创建 samlToken,甚至可以验证它并创建 claimPrincipal。我不确定在此之后要做什么才能登录。我尝试使用HttpContext.GetOwinContext().Authentication.SignIn(new AuthenticationProperties { IsPersistent = true, RedirectUri = "Home/Homepage" },
claimsPrincipal.Identities.ElementAt(0));
,但如果我在那之后检查 Request.IsAuthenticated ,它仍然是错误的。我偷偷怀疑我的 StartupAuth.cs 文件中需要额外的配置,但我不确定,这就是我求助于你们的原因。感谢您的任何帮助!