这是我在身份中注册的代码:
[AllowAnonymous]
public ActionResult Register()
{
var roles = db.Roles.Select(r => new { RoleID = r.Id, RoleName = r.Name }).ToList();
ViewBag.Roles = new SelectList(roles, "RoleID", "RoleName");
return View();
}
//
// POST: /Account/Register
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Register(RegisterViewModel model, HttpPostedFileBase UserPhoto)
{
if (ModelState.IsValid)
{
model.DateRegister = DateTime.Now;
var user = new ApplicationUser
{
UserName = model.UserName,
Name = model.Name,
Family = model.Family,
PhoneNumber = model.PhoneNumber,
Gender = model.Gender,
BirthDay = model.BirthDay,
DateRegister = model.DateRegister,
IsActive = false, …
Run Code Online (Sandbox Code Playgroud) 我在我的 Owin 中配置了一个 webApi 和一个静态文件服务器来获取我们在我的应用程序中需要的一些文件。
public void Configuration(IAppBuilder application)
{
//Other middlewares and configurations
....
application.UseFileServer(new FileServerOptions()
{
RequestPath = new PathString("/myPath1/public"),
FileSystem = new PhysicalFileSystem(m_FolderPathProvider.myPath1FolderPublic)
});
// Attribute routing.
.....
}
Run Code Online (Sandbox Code Playgroud)
这就像一个魅力。我需要的是为另一个路径和另一个不同的物理文件夹声明另一个 FileServer。我害怕的是,如果我以同样的方式来做,我会覆盖这个,我将只有一个。那么如何声明第二个文件服务器呢?
谢谢你。
假设我有hotfound.html页面,我想在找不到页面(或wab api方法)时显示它.
如何在OWIN应用程序中处理它?
谢谢
我正在使用该Microsoft.Owin.Testing
库集成测试我的API在内存中.我已经在OWIN JWT中间件中添加了我的身份验证需求,现在我正在尝试传递生成的令牌来测试需要授权的控制器的请求.我可以向您保证JWT中间件设置正确,因为它在正常使用时工作得很好.但是,我正在观察TestServer.HttpClient
对象的一些奇怪行为.当我在HttpClient上设置默认授权头以传递令牌时,我的测试永远不会通过,因为无法识别令牌.但是,当我使用时TestServer.CreateRequest(...)
,测试正确传递并识别令牌.我更喜欢使用HttpClient方法,因为它们使用所提供的所有扩展方法使事情变得简单得多PostAsJsonAsync
,等等.我开始认为在TestServer.HttpClient
我或者我有一个完全缺少某些东西的错误.
这是我的测试类(使用NUnit3):
public class DefinitionsControllerTests
{
private TestServer _server;
private string _accessToken;
[SetUp]
public void Setup()
{
_server = TestServer.Create<Startup>();
var credentials = new FormUrlEncodedContent(new[] {
new KeyValuePair<string, string>("grant_type", "password"),
new KeyValuePair<string, string>("username", "john.doe@mail.com"),
new KeyValuePair<string, string>("password", "testing123")
});
// get token from OWIN JWT middleware
dynamic resultBody = JObject.Parse(
_server.HttpClient.PostAsync("/oauth/token", credentials).Result.Content.ReadAsStringAsync().Result);
_accessToken = (string)resultBody.access_token;
// this does not appear to ever work
_server.HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", _accessToken);
} …
Run Code Online (Sandbox Code Playgroud) string path = "/SomeSubPath";
app.Map(new PathString(path),
(application) =>
{
app.Run((ctx) =>
{
Debug.WriteLine("{0} : {1}", path, ctx.Request.Path);
return Task.FromResult(0);
});
});
app.Use(async (ctx, next) =>
{
var watch = new Stopwatch();
watch.Start();
await next();
watch.Stop();
Debug.WriteLine("Request handled in {0} [ms]", watch.ElapsedMilliseconds);
});
app.Use(async (ctx, next) =>
{
Debug.WriteLine("{0} : {1}", "/", ctx.Request.Path);
await next();
});
Run Code Online (Sandbox Code Playgroud)
考虑上面的管道配置.
在我的启动类中,我想将"SomeSubPath"映射到一个独有的中间件处理程序.IE浏览器.以"/ SomeSubPath"开头的请求应该由一些独占的中间件处理,而所有其他请求应该由管道的其余部分处理.我正在使用此处找到的IAppBuilder.Map扩展.令我惊讶的是,中间件正在拦截所有请求,从而抑制应用程序的其余部分处理这些请求.