我们假设这是我的行动方法
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状态代码?
当我尝试使用ASP.NET Core Newsoft JSON.NET序列化某些域对象时,它会抛出异常,因为它正在检测自引用循环.
在ASP.NET 4中,我们以这种方式全局修复它: JSON.NET错误检测到类型的自引用循环
我们如何在ASP.NET Core中解决这个问题?
在我看到的默认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)
我正在寻找一种自定义身份验证的正确方法.
我理解这是我们使用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呢?
在身份服务器示例中,我们找到了这样的代码 Startup.cs
var certFile = env.ApplicationBasePath + "\\idsrv3test.pfx";
var signingCertificate = new X509Certificate2(certFile, "idsrv3test");
Run Code Online (Sandbox Code Playgroud)
我如何在生产场景中更换它?
在单元测试中模拟以下代码的最佳方法是什么:
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设置.
任何人都可以帮我解决这个问题,对于上述方法,使用模拟对象的最佳测试方法是什么?
我正在使用像这样的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身份并使用像这样的实体框架将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) 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# ×3
asp.net-core ×2
asp.net-mvc ×1
async-await ×1
controller ×1
json.net ×1
jwt ×1
moq ×1
streamreader ×1
unit-testing ×1