asp.net-web-api – 认证/授权MVC 5和Web API – Katana / Owin
|
我在尝试决定接受我所拥有的项目的路线时遇到了问题. 我一直在阅读OWIN规范和.NET中的Katana实现.我之所以选择Katana路由,是因为与ADFS和令牌/ Cookie生成相关的owin组件. 我有两个项目,一个用于MVC 5网站,另一个用于Web API.它们将来可能会停留在两台独立的服务器上,但现在它们是相同的. 我知道我将使用IIS,所以Owin管道不是我调查的必要条件. 我的要求是将有使用ADFS登录的用户,以及将使用角色/成员资格提供程序使用令牌/ Cookie生成登录的其他用户.根据谁进行身份验证,我的网页的某些部分将被公开.网页工程师用剃须刀完成. 有没有人有任何我可以阅读的材料来帮助解释我可以采取的设计流程?或者任何人都做了类似于我正在经历的项目,可以添加任何建议?有很多不同的文件描述了我需要的特定事物,但不是大局;比如只讨论WebAPI和ADFS,或WebAPI和windows azure等. 我的理论是在MVC5网站项目上实现认证/授权,在Web API上进行授权(两者之间需要以某种方式进行通信).然后,我可能为ADFS创建项目的副本,并为令牌/ cookie身份验证创建另一个副本?或者我可能需要进行4种不同的身份验证:2个用于adfs,我根据MVC5网站和Web API进行身份验证,另外2个用于令牌/ cookie生成. 任何建议都会有所帮助,因为我对这种技术不是很熟悉. 解决方法我可以提供OWIN中的WsFederation选项很好但需要cookie …而且它们与使用cookie的本地身份验证不同. ADFS 2.0 / WsFederation使用AuthenticationType =“Cookies”,本地身份验证使用AuthenticationType =“ApplicationCookie”.就我所知,它们显然是不相容的.我认为你必须为ADFS使用令牌身份验证,但我相信在2012R2上需要ADFS 3.0.为此使用OWIN OAuth.更新:经过一段时间的努力,我已经想出如何让这两种身份验证类型在同一个Web应用程序中和平共存.使用OWIN,设置调用UseCookieAuthentication TWICE,一次启用新的WsFederationAuthentication中间件,再次启用本地cookie身份验证.这不是直观的,但在幕后,为每个指定不同的身份验证类型将它们设置为不同的auth“引擎”.以下是我在Startup中的外观: app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,LoginPath = new PathString("/Account/Login"),Provider = new CookieAuthenticationProvider
{
OnResponseSignIn = ctx =>
{
ctx.Identity = TransformClaims(ctx,app);
}
}
});
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
Provider = new CookieAuthenticationProvider
{
OnResponseSignIn = ctx =>
{
ctx.Identity = TransformClaims(ctx,app);
}
}
});
app.UseWsFederationAuthentication(new WsFederationAuthenticationOptions
{
Wtrealm = Realm,MetadataAddress = Metadata,Caption = "Active Directory",SignInAsAuthenticationType = CookieAuthenticationDefaults.AuthenticationType
});
这成功地允许用户对本地SQL表或ADFS 2.0进行身份验证. TransformClaims标注允许我规范化这两个提供者之间的声明,使它们保持一致. 编辑:这是一个非常基本的TransformClaims.你可以在其中做很多事情:从你的数据库中获取用户,设置导航声明,自定义权限,角色集合等等.我刚刚从一个更大的实现中构建了这个精简版本,所以我没有运行它,但希望你能够了解如何利用OnResponseSignIn事件. private static ClaimsIdentity TransformClaims(CookieResponseSignInContext ctx,IAppBuilder app)
{
var ident = ctx.Identity;
var claimEmail = ident.Claims.SingleOrDefault(c => c.Type == ClaimTypes.Email);
var claimName = ident.Claims.SingleOrDefault(c => c.Type == ClaimTypes.Name);
//normalize my string identifier
var loginString = (claimEmail != null) ? claimEmail.Value : (claimName != null) ? claimName.Value : null;
var efctx = ctx.OwinContext.Get<DBEntities>();
var user = UserBL.GetActiveUserByEmailOrName(efctx,loginString);
if (user == null)
{
//user was auth'd by ADFS but hasn't been auth'd by this app
ident.AddClaim(new Claim(ClaimTypesCustom.Unauthorized,"true"));
return ident;
}
if (ident.Claims.First().Issuer == "LOCAL AUTHORITY")
{
//Local
//local already has claim type "Name"
//local didn't have claim type "Email" - adding it
ident.AddClaim(new Claim(ClaimTypes.Email,user.Email));
}
else
{
//ADFS
//ADFS already has claim type "Email"
//ADFS didn't have claim type "Name" - adding it
ident.SetClaim(ClaimTypes.Name,user.UserName);
}
//now ident has "Name" and "Email",regardless of where it came from
return ident;
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – 存储库是否应该调用另一个存储库?或者存储库是
- asp.net-mvc – 如何找出在MVC3中调用我的视图的控制器动作
- ASP.NET压力测试
- asp.net-mvc – 依赖于另一个字段的属性
- asp.net – 将HTML5占位符文本添加到文本框.net
- asp.net – 检查.NET中两个双精度相等的问题 – 这个方法有
- asp.net-mvc – MVC Razor HTML助手语法:Viewbag in Html.
- asp.net – context.Response.Charset = Encoding.UTF8.ToS
- asp.net-mvc – 全局访问Ninject内核
- asp.net – AppendHeader是否与AddHeader完全相同?
- asp.net-mvc-4 – 如何在ASP.NET MVC 4中从数据库
- 实体框架 – Unity Lifetime Managers和EF Data
- asp.net-mvc-3 – 在Razor / MVC3中显示db的图像
- asp.net-mvc – Json返回时如何读取modelstate错
- 如何利用ASP.net IIS 7.5中的浏览器缓存
- asp.net-mvc – 关于使用重写的AuthorizeAttribu
- asp.net-mvc – 部署AntiforgeryToken错误
- ASP.NET MVC 3中建立的[Email]属性是不是?
- 在asp.net中使用加密数据库联接字符串保证数据安
- asp.net – 如何配置IIS以接受POST请求?
