我已经为Katana实现了一个基本认证中间件(下面的代码).
(我的客户端托管在跨域,然后是实际的API).
如果满足以下条件,浏览器可以跳过预检请求:
请求方法是GET,HEAD或POST,并且应用程序不会设置除Accept,Accept-Language,Content-Language,Content-Type或Last-Event-ID以及Content-Type标头之外的任何请求标头( if set)是以下之一:application/x-www-form-urlencoded multipart/form-data text/plain
在javascript中,我在服务器接受请求的所有请求上设置了认证头(使用jquery,beforeSend).这意味着上面将发送所有请求的选项请求.我不想那样.
function make_base_auth(user, password) {
var tok = user + ':' + password;
var hash = Base64.encode(tok);
return "Basic " + hash;
}
Run Code Online (Sandbox Code Playgroud)
我该怎么做才能解决这个问题?我的想法是在用户身份验证后将用户信息存储在cookie中.
我还在katana项目中看到了Microsoft.Owin.Security.Cookies - 这可能是我想要的而不是我自己的基本身份验证吗?
BasicAuthenticationMiddleware.cs
using Microsoft.Owin;
using Microsoft.Owin.Logging;
using Microsoft.Owin.Security.Infrastructure;
using Owin;
namespace Composite.WindowsAzure.Management.Owin
{
public class BasicAuthenticationMiddleware : AuthenticationMiddleware<BasicAuthenticationOptions>
{
private readonly ILogger _logger;
public BasicAuthenticationMiddleware(
OwinMiddleware next,
IAppBuilder app,
BasicAuthenticationOptions options)
: base(next, options)
{
_logger = app.CreateLogger<BasicAuthenticationMiddleware>();
}
protected override AuthenticationHandler<BasicAuthenticationOptions> CreateHandler()
{
return new …Run Code Online (Sandbox Code Playgroud) 我在Visual Studio 2010中创建了一个控制台应用程序,并为OWIN安装了nuget软件包,如Microsoft.Owin.Hosting和Microsoft.Owin.Host.HttpListener,并配置如下
namespace KatanaIntro
{
class Program
{
static void Main(string[] args)
{
const string uri = "http://localhost:8080";
using (WebApp.Start<Startup>(uri))
{
Console.WriteLine("Started");
Console.ReadKey();
Console.WriteLine("Stopped");
}
}
}
public class Startup
{
public void Configuraion(IAppBuilder app)
{
app.Run(ctx=> ctx.Response.WriteAsync("Welcome to my first katana application"));
}
}
}
Run Code Online (Sandbox Code Playgroud)
运行应用程序后,我得到异常EntryPointNotFoundException *尝试加载应用程序时发生以下错误.- 在类'KatanaIntro.Startup,KatanaIntro,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null*中找不到'配置'方法
我怎么能解决这个问题?我失踪了什么?
Owin/Katana可以通过IIS或自托管来托管.在IIS上托管时,ASP.NET请求生命周期会发生什么?它会像以前一样运行吗?(BeginRequest等...).究竟发生了什么?
我正在测试OWIN中间件,并编写了以下中间件类。但是奇怪的是,当我请求没有任何路径(localhost:<port>)的URL时,我总共调用此中间件大约四次。但是,当我打电话localhost:<port>/welcome.htm或我打电话时,localhost:<port>/default.htm它仅被调用一次,如预期的那样。我想念什么?
public class MyMiddleware : OwinMiddleware
{
public MyMiddleware(OwinMiddleware next)
: base(next)
{
}
public override async Task Invoke(IOwinContext context)
{
await this.Next.Invoke(context);
if (context.Request.Path.HasValue && (context.Request.Path.Value == "/" || context.Request.Path.Value.EndsWith(".htm")))
{
using (var writer = new StreamWriter(context.Response.Body))
{
await writer.WriteLineAsync("Next middleware: " + this.Next.ToString());
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的启动配置:
public void Configuration(IAppBuilder appBuilder)
{
appBuilder.UseErrorPage(new ErrorPageOptions {
ShowCookies = true,
ShowEnvironment = true,
ShowExceptionDetails = true,
ShowHeaders = true,
ShowQuery = true,
ShowSourceCode …Run Code Online (Sandbox Code Playgroud) 使用katana,为什么Startup类不应该实现相应的接口,例如:
interface IStartup
{
void Configuration(IAppBuilder app);
}
public class MyStartup : IStartup
{
public void Configuration(IAppBuilder app)
{
...
}
}
Run Code Online (Sandbox Code Playgroud)
我认为开发人员可以更直观地了解他们应该为WebApp.Start<T>方法提供什么作为T参数而不是猜测和寻找示例,它应该更明确:
public void Start<T>() where T : IStartup
Run Code Online (Sandbox Code Playgroud) 我从Visual Studio 2013获得了一个近乎完整的ASP.NET MVC模板应用程序.它是使用以下设置创建的:
创建>项目> Web> ASP.NET Web应用程序>确定> MVC,个人用户帐户>确定
我正在尝试使用OWIN Google提供商登录并遇到问题.
OWINs Configuration方法如下所示:
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login")
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
var google = new Microsoft.Owin.Security.Google.GoogleOAuth2AuthenticationOptions
{
ClientId = "xxxxx.apps.googleusercontent.com",
ClientSecret = "xxxxxxxxx-yyyyyyyyy"
};
google.Scope.Add("profile");
google.Scope.Add("email");
app.UseGoogleAuthentication(google);
Run Code Online (Sandbox Code Playgroud)
我点击Google网页上的提供商按钮,通过谷歌进行身份验证,然后返回参考ExternalLoginCallback.此时,该过程在此行中出现故障:
var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
loginInfo是null,它将我重定向回登录页面,没有任何可见的错误.
我正在研究Scott Allen关于Pluralsight的MVC 5基础课程
我在下面的代码中"使用(WebApp.Start(uri))"时收到错误.
错误是
An unhandled exception of type 'System.ArgumentException' occurred in Microsoft.Owin.dll
System.ArgumentException was unhandled
HResult=-2147024809
Message=No conversion available between ConsoleApplication1.HelloWorldComponent and System.Func`2[System.Collections.Generic.IDictionary`2[System.String,System.Object],System.Threading.Tasks.Task].
Parameter name: signature
Source=Microsoft.Owin
ParamName=signature
StackTrace:
at Microsoft.Owin.Builder.AppBuilder.Convert(Type signature, Object app)
at Microsoft.Owin.Builder.AppBuilder.BuildInternal(Type signature)
at Microsoft.Owin.Builder.AppBuilder.Build(Type returnType)
at Microsoft.Owin.Hosting.ServerFactory.ServerFactoryAdapter.Create(IAppBuilder builder)
at Microsoft.Owin.Hosting.Engine.HostingEngine.StartServer(StartContext context)
at Microsoft.Owin.Hosting.Engine.HostingEngine.Start(StartContext context)
at Microsoft.Owin.Hosting.Starter.DirectHostingStarter.Start(StartOptions options)
at Microsoft.Owin.Hosting.Starter.HostingStarter.Start(StartOptions options)
at Microsoft.Owin.Hosting.WebApp.StartImplementation(IServiceProvider services, StartOptions options)
at Microsoft.Owin.Hosting.WebApp.Start(StartOptions options)
at Microsoft.Owin.Hosting.WebApp.Start[TStartup](StartOptions options)
at Microsoft.Owin.Hosting.WebApp.Start[TStartup](String url)
at ConsoleApplication1.Program.Main(String[] args) in e:\EShared\Dev2015\WebAppScottAllen\ConsoleApplication1\ConsoleApplication1\Program.cs:line 16
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, …Run Code Online (Sandbox Code Playgroud) 当我写回复时,Katana会跳过发送Elapsed-Time响应头.在我第一次写入流之前,如何让它为我设置标题?
public override async Task Invoke(IOwinContext context)
{
var stopwatch = new Stopwatch();
stopwatch.Start();
await Next.Invoke(context);
stopwatch.Stop();
context.Response.Headers.Add("Elapsed-Time", new[] {stopwatch.ElapsedMilliseconds.ToString()});
}
Run Code Online (Sandbox Code Playgroud)
public override async Task Invoke(IOwinContext context)
{
await context.Response.WriteAsync("test");
}
Run Code Online (Sandbox Code Playgroud) 我试图从Owin请求获取查询字符串参数.虽然此参数位于查询字符串中,但参数"test"的获取操作仍为空.如何从OWIN主机读取请求参数?
呼叫:
localhost:5000/?test=firsttest
Run Code Online (Sandbox Code Playgroud)
码:
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseHandlerAsync((req, res) =>
{
string paramTest = req.Get<string>("test");
return res.WriteAsync(paramTest);
});
}
Run Code Online (Sandbox Code Playgroud) 在某些情况下,例如单页应用程序,最终可能会有多种身份验证方法.例如,您的应用程序可能会使用基于cookie的身份验证来登录JavaScript请求的承载身份验证.在某些情况下,您可能有多个身份验证中间件实例.例如,两个cookie中间件,其中一个包含基本身份,另一个是在多因素身份验证触发时创建的,因为用户请求了需要额外安全性的操作.
例:
app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
AuthenticationScheme = "Cookie",
LoginPath = new PathString("/Account/Unauthorized/"),
AccessDeniedPath = new PathString("/Account/Forbidden/"),
AutomaticAuthenticate = false
});
app.UseBearerAuthentication(options =>
{
options.AuthenticationScheme = "Bearer";
options.AutomaticAuthenticate = false;
});
Run Code Online (Sandbox Code Playgroud)
但是它仅描述了如何使用Bearer或Cookie auth.不清楚的是其他组合是有效的,或者如何正确地向客户发放承载或cookie.
怎么能实现呢?
asp.net asp.net-mvc asp.net-authentication katana asp.net-core
katana ×10
owin ×9
asp.net ×3
c# ×3
.net ×2
asp.net-core ×1
asp.net-mvc ×1
iis ×1
oauth ×1
stream ×1