Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用WebApi与ADFS进行身份验证_C#_Asp.net_Ajax_Asp.net Web Api_Adfs - Fatal编程技术网

C# 使用WebApi与ADFS进行身份验证

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页面返回给客户端,

我们正在尝试创建以下场景:

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

问题:

  • 目前使用的机制似乎笨重。这个机制是正确的还是有更好的方法

  • 若我们使用上面的方法,那个么用户是否有可能编辑收到的html页面,并给自己分配他实际上并没有的角色

  • 即使上述机制是正确的,当WebApi重定向到身份验证时,我们仍然存在cors错误。Cors在WebApi的Startup.cs中启用。我们如何摆脱这个
  • 代码:

    阿贾克斯:

    Startup.Auth.cs:

    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);
                }
        }
    }