小编sun*_*nil的帖子

如何在返回IHttpActionResult时对web api动作方法进行单元测试?

我们假设这是我的行动方法

public IHttpActionResult Get(int id)
{
    var status = GetSomething(id);
    if (status)
    {
        return Ok();
    }
    else
    {
        return NotFound();
    }
}
Run Code Online (Sandbox Code Playgroud)

测试将是

var httpActionResult = controller.Get(1);
Run Code Online (Sandbox Code Playgroud)

如何在此之后检查我的http状态代码?

c# asp.net-web-api asp.net-web-api2

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

在ASP.NET Core中检测到自引用循环

当我尝试使用ASP.NET Core Newsoft JSON.NET序列化某些域对象时,它会抛出异常,因为它正在检测自引用循环.

在ASP.NET 4中,我们以这种方式全局修复它: JSON.NET错误检测到类型的自引用循环

我们如何在ASP.NET Core中解决这个问题?

json.net asp.net-core-mvc asp.net-core

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

如何在asp.net web api 2中自定义对我自己的表集的身份验证?

在我看到的默认AccountController中创建

    public AccountController()
        : this(Startup.UserManagerFactory(), Startup.OAuthOptions.AccessTokenFormat)
    {
    }
Run Code Online (Sandbox Code Playgroud)

在Startup.Auth.cs中我看到了

    UserManagerFactory = () => 
                new UserManager<IdentityUser>(new UserStore<IdentityUser>());   
Run Code Online (Sandbox Code Playgroud)

似乎UserStore的实现来自Microsoft.AspNet.Identity.EntityFramework.

因此,要自定义身份验证,我必须实现自己的UserStore版本

 class MYSTUFFUserStore<IdentityUser> : UserStore<IdentityUser>
 {
 } 
Run Code Online (Sandbox Code Playgroud)

并覆盖方法,然后在Startup.Auth.cs中执行此操作

UserManagerFactory = () => 
               new UserManager<IdentityUser>(new MYSTUFFUserStore<IdentityUser>());   
Run Code Online (Sandbox Code Playgroud)

我正在寻找一种自定义身份验证的正确方法.

asp.net-web-api asp.net-identity

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

使用Entity Framework调用没有任何内容返回的存储过程

我理解这是我们使用Entity Framework调用存储过程的方式.

context.Database.SqlQuery<myEntityType>(
    "mySpName @param1, @param2, @param3",
    new SqlParameter("param1", param1),
    new SqlParameter("param2", param2),
    new SqlParameter("param3", param3)
);
Run Code Online (Sandbox Code Playgroud)

但是,如果我的存储过程只有一对更新语句并且不返回任何内容,那么我应该放什么myEntityType呢?

entity-framework

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

我将如何生成Identity Server签名证书

在身份服务器示例中,我们找到了这样的代码 Startup.cs

var certFile = env.ApplicationBasePath + "\\idsrv3test.pfx";

var signingCertificate = new X509Certificate2(certFile, "idsrv3test");
Run Code Online (Sandbox Code Playgroud)

我如何在生产场景中更换它?

identityserver3 identityserver4

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

使用MOQ对象进行ASP.NET MVC单元测试

在单元测试中模拟以下代码的最佳方法是什么:

public ActionResult Products()
{
      ViewBag.Title = "Company Product";                        
      IEnumerable<ProductDetailDto> productList =   ProductService.GetAllEffectiveProductDetails();
      ProductModels.ProductCategoryListModel model = new ProductModels.ProductCategoryListModel 
      {     
            //the type of ProductDetails => IEnumerable<productDetailDto>  
            ProductDetails = ProductService.GetAllEffectiveProductDetails(),

            //the type of ProductCategoryList => IEnumerable<selectlistitem>
            ProductCategoryList = productList.Select(x => new SelectListItem
            {
                Value = x.FKProductId.ToString(),
                Text = x.Name
            })
      };
      return View(model);
}
Run Code Online (Sandbox Code Playgroud)

仅供参考,我正在开发VS 2012,MVC 4.0,使用MOQ对象进行单元测试和TFS设置.

任何人都可以帮我解决这个问题,对于上述方法,使用模拟对象的最佳测试方法是什么?

unit-testing controller moq asp.net-mvc-4

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

UseJwtBearerAuthentication在令牌到期时返回HTTP 500

我正在使用像这样的UseJwtBearerAuthentication

app.UseJwtBearerAuthentication(options =>
{
   options.Authority = Configuration["Urls:IdentityServer"];
   options.RequireHttpsMetadata = false;

   options.Audience = Configuration["Urls:IdentityServer"] + "/resources";
   options.AutomaticAuthenticate = true;
   options.Events = new JwtBearerEvents
   {
        OnAuthenticationFailed = context =>
        {
          context.HandleResponse();   
          return Task.FromResult(0);
        }
   }; 
});
Run Code Online (Sandbox Code Playgroud)

在visual studio的诊断窗口中,我看到以下两个例外:

System.IdentityModel.Tokens.dll中的System.IdentityModel.Tokens.SecurityTokenExpiredException'("IDX10223:生命周期验证失败.令牌已过期.

并下线

抛出异常:Microsoft.AspNet.Authentication.dll中的"System.ArgumentNullException"("值不能为空.")

如何返回HTTP 401 Unauthorized?

asp.net-mvc jwt identityserver3 asp.net-core

3
推荐指数
1
解决办法
2868
查看次数

如何重新配置​​实体框架FirstOrDefaultAsync

使用ASP.NET身份并使用像这样的实体框架UserStore自定义到我自己的数据库表集时

public Task<MyUser> FindByNameAsync(string userName)
{
   var context = new CompanyDbContext();

   Task<MyUser> task = context.MyUsers.Where(u => u.UserName == userName)
                                      .FirstOrDefaultAsync();

   return task;
}
Run Code Online (Sandbox Code Playgroud)

并调用它像这样工作正常:

MyUser user = await userManager.FindAsync(context.UserName, context.Password);
Run Code Online (Sandbox Code Playgroud)

现在,我想用硬编码的用户列表替换它,如下所示:

public Task<MyUser> FindByNameAsync(string userName)
{
   var myusers = new List<MyUser> 
   { 
       new MyUser() { Id="1", UserName = "tom", Password = "secret" },
       new MyUser() { Id="2", UserName = "mary", Password = "supersecret" }
   };

   Task<MyUser> task = new Task<MyUser>(() => myusers.SingleOrDefault(
                                                  u => u.UserName == …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework async-await asp.net-identity

2
推荐指数
1
解决办法
5102
查看次数

当我提前关闭HttpWebResponse时,Streamreader不起作用

Uri targetUri = new Uri(targetURL);    
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(targetUri);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string data = reader.ReadToEnd();
response.Close();
Run Code Online (Sandbox Code Playgroud)

为什么上面的代码工作正常,但以下代码没有?请注意,我在以下代码中提前关闭响应.

Uri targetUri = new Uri(targetURL);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(targetUri);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
response.Close();
string data = reader.ReadToEnd();
Run Code Online (Sandbox Code Playgroud)

c# httpwebresponse streamreader

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