wir*_*_in 38 c# asp.net asp.net-mvc owin asp.net-mvc-5
我是OWIN身份验证的新手,我必须误解一切是如何工作的,但我无法在任何地方找到这个.
我想要的是能够使用中央域进行身份验证.如果有人apps.domain.com在未经过身份验证时尝试访问,则会将其重定向到accounts.domain.com/login以便将所有身份验证分成其自己的域和应用程序.使用MVC 4表单身份验证非常容易,您可以在其中指定完整的URL,但似乎不适用于OWIN.
在Startup.Auth.cs:
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
LoginPath = new PathString("/account/login")
}
Run Code Online (Sandbox Code Playgroud)
使用该CookieDomain选项设置cookie时,可以轻松指定域.但是,当您指定要重定向到的登录路径时,它必须相对于当前应用程序,那么如何在MVC 4表单身份验证中完成哪些操作非常简单?
在没有深入了解OWIN身份验证的全部内容的情况下,经过几个小时的搜索后,我找不到任何解决此问题的方法.
Kév*_*let 49
public class Startup
{
public void Configuration(IAppBuilder app)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationMode = AuthenticationMode.Active,
LoginPath = new PathString("/account/login"),
LogoutPath = new PathString("/account/logout"),
Provider = new CookieAuthenticationProvider
{
OnApplyRedirect = ApplyRedirect
},
});
}
private static void ApplyRedirect(CookieApplyRedirectContext context)
{
Uri absoluteUri;
if (Uri.TryCreate(context.RedirectUri, UriKind.Absolute, out absoluteUri))
{
var path = PathString.FromUriComponent(absoluteUri);
if (path == context.OwinContext.Request.PathBase + context.Options.LoginPath)
{
context.RedirectUri = "http://accounts.domain.com/login" +
new QueryString(
context.Options.ReturnUrlParameter,
context.Request.Uri.AbsoluteUri);
}
}
context.Response.Redirect(context.RedirectUri);
}
}
Run Code Online (Sandbox Code Playgroud)
如果apps.domain.com是唯一返回URL基地可能的话,你应该认真考虑更换context.Request.Uri.AbsoluteUri与context.Request.PathBase + context.Request.Path + context.Request.QueryString和验证服务器建立绝对回报的网址,以保护您的应用中滥用重定向.
希望这可以帮助 ;)
编辑:您可能会问自己为什么我不直接使用该context.RedirectUri属性应用重定向.事实上,ICookieAuthenticationProvider.ApplyRedirect负责多个重定向,对应于登录和注销流程(是的,我知道,它违反了单一责任原则......).但更糟糕的是:context.RedirectUri当cookie被有效地发送回浏览器时,可以在登录流程的开头表示认证端点的绝对URL,也可以表示最终浏览器的目的地(即真正的相对"返回URL"). ..这就是为什么我们需要确保它context.RedirectUri是绝对的并且对应于已注册的context.Options.LoginPath.
我正在研究https://github.com/IdentityServer/IdentityServer3的示例,我有一个不同的答案.在https://www.scottbrady91.com/Identity-Server/Identity-Server-3-Standalone-Implementation-Part-2的示例中,它们显示了一个使用独立IdP和cookie身份验证的MVC应用程序.这个例子没有包括让401重定向工作,但我偶然发现了.
基本方案是在AccountController中创建一个用于登录的操作.
public ActionResult SignIn() {
// set up some bookkeeping and construct the URL to the central auth service
return Redirect(authURL);
}
Run Code Online (Sandbox Code Playgroud)
现在您有一个可以在Startup中使用的本地URL
public class Startup {
public void Configuration(IAppBuilder app) {
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = "Cookies",
LoginPath = new PathString("/Account/SignIn")
});
}
Run Code Online (Sandbox Code Playgroud)
对于想要在401之前登录的人,您还可以在菜单栏上为SignIn添加操作链接,这也是一个额外的好处.我们在这里所做的就是解决了在做什么时做出的决定.未经身份验证的用户根据如何获取身份验证来请求资源.
| 归档时间: |
|
| 查看次数: |
11776 次 |
| 最近记录: |