C# 使用WebApi与ADFS进行身份验证
我们正在尝试创建以下场景: Html页面向WebApi发送ajax请求,询问用户是否具有特定的用户角色。WebApi从ADFS检查用户是否已登录(如果未登录,WebApi将对用户进行身份验证)。然后WebApi从ADF读取用户角色,并将true/false返回到html页面 到目前为止我们拥有的: Ajax向WebApi发送Get请求。在WebApi中,我们有Authorize标记,它正确地将用户发送到ADFS身份验证。但是,在身份验证之后,ADFS将包含saml信息的html页面返回给客户端,而不是WebApi。因此,现在我们创建另一个ajax请求(这次发布),它已将html页面作为数据接收。然后,WebApi将对此进行解析,并根据SAML响应中的用户角色返回true/false 问题:C# 使用WebApi与ADFS进行身份验证,c#,asp.net,ajax,asp.net-web-api,adfs,C#,Asp.net,Ajax,Asp.net Web Api,Adfs,我们正在尝试创建以下场景: Html页面向WebApi发送ajax请求,询问用户是否具有特定的用户角色。WebApi从ADFS检查用户是否已登录(如果未登录,WebApi将对用户进行身份验证)。然后WebApi从ADF读取用户角色,并将true/false返回到html页面 到目前为止我们拥有的: Ajax向WebApi发送Get请求。在WebApi中,我们有Authorize标记,它正确地将用户发送到ADFS身份验证。但是,在身份验证之后,ADFS将包含saml信息的html页面返回给客户端,
public void ConfigureAuth(IAppBuilder app)
{
app.SetDefaultSignInAsAuthenticationType(WsFederationAuthenticationDefaults.AuthenticationType);
app.UseWsFederationAuthentication(new WsFederationAuthenticationOptions
{
MetadataAddress = ConfigurationManager.AppSettings["ida:AdfsMetadataEndpoint"],
Wtrealm = ConfigurationManager.AppSettings["ida:Audience"],
});
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = WsFederationAuthenticationDefaults.AuthenticationType
});
}
用户控制器:
namespace SSOWebApiSample.Controllers{
[RoutePrefix("api/user")]
public class UserController : ApiController
{
[HttpGet]
[Route("")]
[Authorize]
public IHttpActionResult GetUser()
{
return Ok();
}
[HttpPost]
[Route("")]
public async Task<IHttpActionResult> PostUser()
{
bool isAdditionalInfoAllowedUser = false;
string result = await Request.Content.ReadAsStringAsync();
//Parse result here
return Ok(isAdditionalInfoAllowedUser);
}
}
}
namespace-SSOWebApiSample.Controllers{
[RoutePrefix(“api/用户”)]
公共类用户控制器:ApiController
{
[HttpGet]
[路线(“”)
[授权]
公共IHttpActionResult GetUser()
{
返回Ok();
}
[HttpPost]
[路线(“”)
公共异步任务姿态器()
{
bool isAdditionalInfoAllowedUser=false;
字符串结果=wait Request.Content.ReadAsStringAsync();
//在这里解析结果
返回Ok(isAdditionalInfoAllowedUser);
}
}
}
AdalJS会很好地解决这个问题。请参阅以下资料:
要使用ADFS身份验证成功调用CORS Web API,我发现在我的Angular应用程序的配置中调用adalAuthenticationService.init()时,需要设置实例、租户、clientId和端点成员。请参见示例2了解端点示例,但将GUID替换为URL。AdalJS将很好地解决此问题。请参阅以下资料: 要使用ADFS身份验证成功调用CORS Web API,我发现在我的Angular应用程序的配置中调用adalAuthenticationService.init()时,需要设置实例、租户、clientId和端点成员。有关端点示例,请参见示例2,但将GUID替换为URL
namespace SSOWebApiSample.Controllers{
[RoutePrefix("api/user")]
public class UserController : ApiController
{
[HttpGet]
[Route("")]
[Authorize]
public IHttpActionResult GetUser()
{
return Ok();
}
[HttpPost]
[Route("")]
public async Task<IHttpActionResult> PostUser()
{
bool isAdditionalInfoAllowedUser = false;
string result = await Request.Content.ReadAsStringAsync();
//Parse result here
return Ok(isAdditionalInfoAllowedUser);
}
}
}