C# 如何使用OWIN登录,并使用ASP.NET MVC从ADFS返回SAML票证?
我有一个网站,它有自己的登录屏幕,用户在其中键入用户名和密码。点击登录按钮后,应该通过ADFS对用户进行身份验证,我应该获得一个SAML令牌。这很有效。不过现在,我不确定我需要做什么来登录用户。我不确定的原因是因为这个网站有点扭曲。正如我所说,一个用户通过我们网站的登录页面通过ADFS登录。但是,如果用户访问未经授权的页面,我们需要将用户重定向到我们的ADFS登录页面,而不是将用户重定向到我们的登录页面。我是这样开始整个项目的。用户通过ADFS的登录页面进行身份验证。这只需通过如下方式设置我的ConfigureAuth方法(在StartupAuth.cs中)即可完成:C# 如何使用OWIN登录,并使用ASP.NET MVC从ADFS返回SAML票证?,c#,asp.net-mvc,authentication,claims-based-identity,owin-middleware,C#,Asp.net Mvc,Authentication,Claims Based Identity,Owin Middleware,我有一个网站,它有自己的登录屏幕,用户在其中键入用户名和密码。点击登录按钮后,应该通过ADFS对用户进行身份验证,我应该获得一个SAML令牌。这很有效。不过现在,我不确定我需要做什么来登录用户。我不确定的原因是因为这个网站有点扭曲。正如我所说,一个用户通过我们网站的登录页面通过ADFS登录。但是,如果用户访问未经授权的页面,我们需要将用户重定向到我们的ADFS登录页面,而不是将用户重定向到我们的登录页面。我是这样开始整个项目的。用户通过ADFS的登录页面进行身份验证。这只需通过如下方式设置我的C
public void ConfigureAuth(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseWsFederationAuthentication(
new WsFederationAuthenticationOptions
{
Wtrealm = realm,
MetadataAddress = adfsMetadata
});
}
[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");
}
很简单!如果我尝试转到[Authorized]
页面,它会将我带到ADFS登录页面。在此之后,是时候研究另一种登录方法(通过使用登录页面使用ADFS进行身份验证)。请记住,无论您尝试哪种方式进行身份验证,您仍然使用相同的ADF进行身份验证;一个是使用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>
它可以成功地检索和创建samlToken,甚至可以验证它并创建claimsPrincipal。我不确定在这一点之后要做什么才能登录。我尝试使用HttpContext.GetOwinContext().Authentication.sign(新的AuthenticationProperties{IsPersistent=true,RedirectUri=“Home/Homepage”},
claimsPrincipal.identifies.ElementAt(0))代码>,但如果在此之后检查Request.IsAuthenticated,则仍然为false。我暗自怀疑我的StartupAuth.cs文件中需要额外的配置,但我不确定,这就是为什么我要求助于你们。谢谢你的帮助