我正在使用MVC5和新的OWIN认证中间件尝试VS2013 RC中的一些新功能.
所以,我习惯使用[Authorize]
属性来限制角色的操作,但我正在尝试使用基于声明/活动的授权,而我找不到它的等效属性.
有一个明显的我错过了或者我需要自己动手吗?我有点希望有一个开箱即用.
我正在寻找的具体是[Authorize("ClaimType","ClaimValue")]
我想的.
提前致谢.
我在我的应用程序中使用了OWIN身份验证.
登录操作
var claims = new List<Claim>();
claims.Add(new Claim(ClaimTypes.Name, result.UserFirstName));
claims.Add(new Claim(ClaimTypes.Sid, result.UserID.ToString()));
var identity = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie);
Run Code Online (Sandbox Code Playgroud)
我想从不同的操作访问UserName和UserID.如何访问声明中添加的值?
更新 我试过了
var claims = new List<Claim>();
claims.Add(new Claim(ClaimTypes.Name, result.UserFirstName + " " + result.UserLastName));
claims.Add(new Claim(ClaimTypes.Sid, result.UserIDNumber.ToString()));
var identity = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie);
var authenticationManager = Request.GetOwinContext().Authentication;
authenticationManager.SignIn(identity);
var claimsPrincipal = new ClaimsPrincipal(identity);
Thread.CurrentPrincipal = claimsPrincipal;
Run Code Online (Sandbox Code Playgroud)
我可以在快速窗口中查看值.但即使我无法访问该值.如何获得价值?
我正在使用WSO2 Identity Server进行单点登录实现.
在我的演示应用程序中,我试图从我自己的JDBC数据库中获取经过身份验证的用户的自定义声明属性.
我关注了Pushpalanka的这个博客.
这适用于Identity Server 5.0.0
但是当我使用最新更新"WSO2-IS-5.0.0-SP01"更新Identity Server时,自定义声明处理已停止工作.
以下是错误堆栈:
[2015-04-22 19:09:43,411] ERROR {org.wso2.carbon.identity.application.authentication.framework.handler.sequence.impl.DefaultStepBasedSequenceHandler} - 声明处理失败!org.wso2.carbon.identity.application.authentication.framework.exception.FrameworkException:索引:0,大小:0,位于com.wso2的com.wso2.sample.claim.handler.CustomClaimHandler.handleLocalClaims(CustomClaimHandler.java:200) .sample.claim.handler.CustomClaimHandler.handleClaimMappings(CustomClaimHandler.java:66)在org.wso2.carbon.identity.application.authentication.framework.handler.sequence.impl.DefaultStepBasedSequenceHandler.handleClaimMappings(DefaultStepBasedSequenceHandler.java:604)在有机.wso2.carbon.identity.application.authentication.framework.handler.sequence.impl.DefaultStepBasedSequenceHandler.handlePostAuthentication(DefaultStepBasedSequenceHandler.java:394)在org.wso2.carbon.identity.application.authentication.framework.handler.sequence.impl. DefaultStepBasedSequenceHandler.handle(DefaultStepBasedSequenceHandler.java:134)at org.wso2.carbon.identity.application.authentication.framework.handler.request.impl.DefaultAuthenticationRequestHandler.handle(DefaultAuthenticationReques)tHandler.java:121)在org.wso2.carbon.identity.application.authentication的org.wso2.carbon.identity.application.authentication.framework.handler.request.impl.DefaultRequestCoordinator.handle(DefaultRequestCoordinator.java:94). framework.servlet.CommonAuthenticationServlet.doPost(CommonAuthenticationServlet.java:54)在javax.servlet.http.HttpServlet.service(HttpServlet.java:755)在javax.servlet.http.HttpServlet.service(HttpServlet.java:848)在组织位于org.eclipse.equinox.http的org.eclipse.equinox.http.servlet.internal.ServletRegistration.service(ServletRegistration.java:61)的.eclipse.equinox.http.helper.ContextPathServletAdaptor.service(ContextPathServletAdaptor.java:37)位于org.eclipse.equinox.http.servlet.internal.ProxyServlet.service(ProxyServlet.java:60)的.avlet.internal.ProxyServlet.processAlias(ProxyServlet.java:128),位于javax.servlet.http.HttpServlet.service(HttpServlet) .java:848)org.apac的org.wso2.carbon.tomcat.ext.servlet.DelegationServlet.service(DelegationServlet.java:68)he.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)位于org.wso2.carbon.tomcat.ext.filter.CharacterSetFilter的org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210).的doFilter(CharacterSetFilter.java:61)在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)在org.apache .catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java :472)org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)at org.wso2.carbon.tomcat. org.ws上的ext.valves.CompositeValve.continueInvocation(CompositeValve.java:178)在org.wso2.carbon.tomcat的org.wso2.carbon.webapp.mgt.TenantLazyLoaderValve.invoke(TenantLazyLoaderValve.java:56)上的o2.carbon.tomcat.ext.valves.CarbonTomcatValve $ 1.invoke(CarbonTomcatValve.java:47)位于org.wso2.carbon.tomcat.ext.valves的org.wso2.carbon.tomcat.ext.valves.CompositeValve.invoke(CompositeValve.java:141)的.ext.valves.TomcatValveContainer.invokeValves(TomcatValveContainer.java:47) .CarbonStuckThreadDetectionValve.invoke(CarbonStuckThreadDetectionValve.java:156)org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)org.wso2.carbon.tomcat.ext.valves.CarbonContextCreatorValve.invoke(CarbonContextCreatorValve.java) :52)org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)org.apache.coyote.http11. AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)at org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProt)ocol.java:589)在java.util的java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)的org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.run(NioEndpoint.java:1653) .concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:615)at java.lang.Thread.run(Thread.java:745)引起:java.lang.IndexOutOfBoundsException:索引:0,大小:0,java.util位于org.wso2.carbon.claim.mgt.ClaimManagerHandler.validateClaims(ClaimManagerHandler.java:668)的java.util.ArrayList.get(ArrayList.java:411)中的.ArrayList.rangeCheck(ArrayList.java:635). wso2.carbon.claim.mgt.ClaimManagerHandler.getMappingsFromOtherDialectToCarbon(ClaimManagerHandler.java:529)在org.wso2.carbon.claim.mgt.ClaimManagerHandler.getMappingsMapFromOtherDialectToCarbon(ClaimManagerHandler.java:614)在com.wso2.sample.claim.handler. CustomClaimHandler.handleLocalClaims(CustomClaimHandler.java:141).
根据我对Identity Server源代码的研究,这个问题出现在org.wso2.identity.application.authentication.framewotk组件的 Authentication Framework中.
问题可能在于验证声明,但我没有在源代码中找到任何名为validateClaims的方法.
在博客文章中给出的源代码中,使用了Authentication Framework版本 - 4.2.2.
我尝试使用最新版本的Authentication Framework - 4.2.3.
但问题仍然存在于同一个组件中.
我仍然在努力.我需要一些指导.
如果我遗失了某些事情,或者你们中的任何人遇到同样的问题,请有人帮忙.
谢谢.
我在我的rc1项目中做过:
User.Claims.ElementAt(#).Value
Run Code Online (Sandbox Code Playgroud)
但是在我切换到rtm后它就不再起作用了.当我调试Razor视图时,对象看起来相同,但User.Claims只是空的.不知道原因是什么.
给定一组特定的权限,例如EditPage,CreateProject,ModifyUser,我目前正在研究创建一些自定义声明类型的两种不同方法来模拟此行为.我可以在网上找到关于最佳方法的小信息,并希望得到一些关于如何在自己的系统中完成此操作的反馈.
我考虑的第一种方法是使用"操作"声明类型,其中特定操作由声明的值指定:
var claims = new []
{
new Claim("http://schemas.company.com/claims/project/action", "EditPage"),
new Claim("http://schemas.company.com/claims/project/action", "CreateProject"),
new Claim("http://schemas.company.com/claims/project/action", "ModifyUser")
}
Run Code Online (Sandbox Code Playgroud)
第二种方法是使用声明类型本身来定义正在执行的操作,不使用该值.这就像"PossessProperty"安全风格,只要用户具有声明类型,他们就可以执行操作.
var claims = new []
{
new Claim("http://schemas.company.com/claims/project/editpage", ""),
new Claim("http://schemas.company.com/claims/project/createproject", ""),
new Claim("http://schemas.company.com/claims/project/modifyuser", "")
}
Run Code Online (Sandbox Code Playgroud)
另请注意,在上面的索赔类型中,我包含了一个"项目"鉴别器,以便我可以区分可以编辑项目A中的页面而不是项目B的用户.
我们还计划将所有这些自定义声明存储在中央"授权"数据库中,因此需要唯一性.
任何想法或反馈将不胜感激.
我有一些ASP.NET MVC
控制器代码,用于检查用户是否经过身份验证,如果是,则会检查是否有特定声明.工作良好.
我有一些单元测试,我需要模拟一个IPrincipal
(这很容易做到)......但我不确定如何检查索赔!我经常这样做
public static ClaimsPrincipal ClaimsPrincipal(this Controller controller)
{
return controller.User as ClaimsPrincipal;
}
Run Code Online (Sandbox Code Playgroud)
和一些控制器代码......
this.ClaimsPrincipal().HasClaim(x => x.......);
Run Code Online (Sandbox Code Playgroud)
但是当我在我的单元测试中测试时,这一切都失败了.因为我不知道我怎么能mock
这样做ClaimsPrincipal
有任何想法吗?
.net asp.net-mvc claims-based-identity claims asp.net-identity
案例:我有一个MVC5应用程序(基本上是带有脚手架视图的MVC5模板),启用了Google身份验证方法.该应用程序已经被配置成接受电子邮件作为用户名和存储来自谷歌分配了类似的姓,给定名称,电子邮件的NameIdentifier等方面的要求,对会员数据库(AspNetUserClaims).
当我注册并使用"本地"用户登录时,一切都很好.如果我使用Google用户登录,则可以.如果我使用设置为同时具有本地和外部登录的帐户登录,则会收到以下错误.
我尝试使用Application_Start中的AntiForgeryConfig选项将令牌的类型更改为不同的设置(示例)
AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.Email;
Run Code Online (Sandbox Code Playgroud)
但是,当组合本地和外部登录时,似乎所有声明都是重复的.最奇怪的是,声明集合(我假设有答案)对于组合和外部唯一登录是相同的.
以本地用户身份登录时,将分配这些声明
[0]: {http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier: a71ff9c0-8dc4-478b-a6f1-2c4cc34b1e46}
[1]: {http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name: some@email.com}
[2]: {http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider: ASP.NET Identity}
Run Code Online (Sandbox Code Playgroud)
使用仅限远程帐户或组合帐户登录时,claim-list如下所示
[0]: {http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier: 4ab33d77-c2a0-4eff-a759-5cca4323ecbf}
[1]: {http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name: some.other@email.com}
[2]: {http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider: ASP.NET Identity}
[3]: {http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier: https://www.google.com/accounts/o8/id?id=AitOGoogleIdentifierRemovedForPrivacygwgwgw}
[4]: {http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress: some.other@email.com}
[5]: {http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname: Other}
[6]: {http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname: Some}
[7]: {http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name: Some Other Person}
Run Code Online (Sandbox Code Playgroud)
任何帮助都感激不尽!
Server Error in '/' Application.
Sequence contains more than one matching element
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more …
Run Code Online (Sandbox Code Playgroud) 我正在使用带有Owin身份的MVC5.
我正在努力在regenerateIdentityCallback中重用任何自定义声明.
我在Startup中有这个配置(在新MVC项目的标准模板中提供)
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser, Guid>(
validateInterval: TimeSpan.FromSeconds(10),
regenerateIdentityCallback: (manager, user) => user.GenerateUserIdentityAsync(manager),
getUserIdCallback: (user) => Guid.Parse(user.GetUserId()))
}
});
Run Code Online (Sandbox Code Playgroud)
GenerateUserIdentityAsync看起来像这样:(以及模板中的标准)
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser, Guid> manager)
{
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// I want here instead of generating new one, just reuse the previous one
userIdentity.AddClaim(new Claim(ClaimTypes.Sid, Guid.NewGuid().ToString()));
return userIdentity;
}
Run Code Online (Sandbox Code Playgroud)
问题是,我无法重复索赔,而且我总是必须为它获得新的价值.在我看到Identity dll的调查之后this
,该用户的实例没有声明,因为它是来自数据库的新用户,并且userIdentity
只有标准声明为Id和UserName,它们是由CreateIdentityAsync方法创建的.从HttpContext.Current获取用户是不可能的,在这个地方它是null.
重用Claim以保留Cookie中的某些值的最佳方法是什么?我可能误解了索赔的目的.请事先提供帮助
我正在使用ASP.NET身份与几个外部登录提供程序,我需要处理以下方案:
1)用户使用外部服务登录(假设它是Facebook),应用程序从Facebook获取一些信息(姓名,电子邮件,出生日期等等).包含此信息的声明被添加到身份.
2)对于以下场景,我需要将此信息存储在应用程序Db中:
管理员浏览已注册用户的列表
电子邮件订阅服务将使用名字和姓氏
问题是如果用户将更新他/她的Facebook个人资料(例如,更改电子邮件地址) - 在这种情况下我需要更新我的Db中的信息(我在AspNetUserClaims表中存储外部声明).实际上,每次外部用户通过身份验证时我都需要更新它.
这是第一次如何保存它的代码:
扩展索赔类:
public class ApplicationUserClaim : IdentityUserClaim<Guid>
{
public string Issuer { get; set; }
public string ClaimValueType { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
启动:
var facebookOptions = new FacebookAuthenticationOptions {
AppId = "...",
AppSecret = "...",
Provider = new FacebookAuthenticationProvider {
OnAuthenticated = (context) => {
context.Identity.AddClaims(new[] {
new Claim("LastName", context.User["last_name"].ToString(), ClaimValueTypes.String, "Facebook"),
new Claim("FirstName", context.User["first_name"].ToString(), ClaimValueTypes.String, "Facebook"),
//...Other claims
});
}
}
};
facebookOptions.Scope.Add("email");
facebookOptions.Scope.Add("user_birthday");
app.UseFacebookAuthentication(facebookOptions);
Run Code Online (Sandbox Code Playgroud)
AuthController
外部登录回调:
public ActionResult …
Run Code Online (Sandbox Code Playgroud) 从brockallen的文章中,他说"AuthenticateAsync()可能会被多次调用",这可能是为什么TransformAsync()被多次调用的原因(在我的应用程序中两次).
我没有得到的是:
有人知道为什么ClaimsIdentity会这样吗?
更新的问题:
我没有得到的是: