小编Ten*_*eim的帖子

使用ADFS对ASP.NET Web Api进行身份验证

我遇到的情况是我需要访问使用ADFS进行身份验证的ASP.NET Web Api.我可以通过浏览器通过ADFS登录门户网站获取相关的FedAuth cookie,从而可靠地点击它.不幸的是,我需要从专用浏览器外部访问它,以便在移动应用中使用.该项目几乎是为工作和学校认证(内部部署)设置的标准visual studio web api模板的略微修改版本,并设置用于cookie认证.

来自Startup.Auth.cs的一些代码:

public void Configuration(IAppBuilder app)
{
    app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);
    app.UseWsFederationAuthentication(
        new WsFederationAuthenticationOptions
        {
            Wtrealm = realm,
            MetadataAddress = adfsMetadata
        });
    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        AuthenticationType = WsFederationAuthenticationDefaults.AuthenticationType
    });
}
Run Code Online (Sandbox Code Playgroud)

我似乎无法弄清楚从哪里开始.我已尝试从ADFS请求访问令牌,并可使用相关登录信息获取不同版本的SAML断言,但它会被Web API拒绝.我误解了它应该如何工作吗?

从我的理解,它应该是这样的: 我认为它应该工作

  1. 应用程序从ADFS请求身份验证令牌
  2. 如果提供的信息正确,ADFS会向被请求者提供身份验证令牌
  3. 应用程序向Web API发出请求,并在名为FedAuth的cookie中发送令牌(默认情况下,无论如何)作为base64编码的字符串
  4. Web Api将令牌发送到ADFS以查明令牌是否正确.
  5. ADFS以某种成功的方式回应
  6. Web Api会根据身份验证的方式对拒绝或数据进行响应.

这就是我现在正在试图弄清楚如何获得正确的令牌.

using System;
using System.IdentityModel.Protocols.WSTrust;
using System.IdentityModel.Tokens;
using System.Net;
using System.Net.Http;
using System.ServiceModel;
using System.ServiceModel.Security;
using Thinktecture.IdentityModel.Extensions;
using Thinktecture.IdentityModel.WSTrust;

namespace ConsoleApplication1
{    
    class Program
    {
        private const string UserName     = "USERNAME";
        private const string …
Run Code Online (Sandbox Code Playgroud)

c# adfs access-token asp.net-web-api

7
推荐指数
1
解决办法
1万
查看次数

ASP.NET MVC Core ViewModel 回发,包括 IFormFile

在 ASP.NET MVC Core 中回发视图模型时是否可能以某种方式不清除文件输入?我知道 IFormFile 不包含指定所选文件目录的成员。

因此,如果视图模型在 ModelState 无效的情况下被发送回,则文件输入将被清除。是否可以在不完全诉诸客户端验证的情况下避免这种情况?

这是一个示例控制器和模板,可以更充分地解释我所说的内容

控制器:

namespace Example
{
    public class CreateVm
    {
        public IFormFile aFile{ get; set; }
        public string someText{get; set;}
    }

    public class ExampleController : Controller
    {
        public IActionResult Action()
        {
            var vm = new CreateVm();
            return View(vm);
        }

        [HttpPost]
        public IActionResult Create(CreateVm vm)
        {
            // Something invalidates the model state
            // The supplied viewmodel isn't valid, send it back
            if (!ModelState.IsValid)
                return View(vm);

            // Do stuff here
            return RedirectToAction(nameof(Create));
        } …
Run Code Online (Sandbox Code Playgroud)

c# asp.net asp.net-mvc asp.net-core-mvc

5
推荐指数
0
解决办法
958
查看次数