在asp.net中添加声明到表单身份验证

use*_*210 7 asp.net asp.net-mvc

我一直在研究一个asp.net应用程序,它使用windows azure表作为用户信息的数据存储.我有一个类,它可以插入和创建表并处理其他东西.当我看到用户已经存在时,我想使用类似的东西发出身份验证令牌

FormsAuthentication.SetAuthCookie(user,true)  
Run Code Online (Sandbox Code Playgroud)

我还想添加用户从windows azure表存储中获得的声明,以便稍后我可以使用类似的东西读取它们

ClaimsPrincipal claimsPrincipal = Page.User as ClaimsPrincipal; 
Run Code Online (Sandbox Code Playgroud)

有人可以建议我如何实现这一目标吗?我完成了这个自定义应用程序的其他部分,但不是很清楚如何使这部分工作.

谢谢

Wik*_*hla 5

几乎一样简单,用SessionAuthenticationModule的帮助.

SessionAuthenticationModule sam = 
    (SessionAuthenticationModule)
    this.Context.ApplicationInstance.Modules["SessionAuthenticationModule"];

IClaimsPrincipal principal = 
   new ClaimsPrincipal( new GenericPrincipal( new GenericIdentity( txtUserName.Text ), null ) );

// create any userdata you want. by creating custom types of claims you can have
// an arbitrary number of your own types of custom data
principal.Identities[0].Claims.Add( new Claim( ClaimTypes.Email, "foo@bar.com" ) );
principal.Identities[0].Claims.Add( new Claim( ClaimTypes.UserData, ReallyLongUserData ) );

var token = 
  sam.CreateSessionSecurityToken( 
     principal, null, DateTime.Now, DateTime.Now.AddMinutes( 20 ), false );
sam.WriteSessionTokenToCookie( token );

Response.Redirect( this.Context.Request.QueryString["ReturnUrl"] );
Run Code Online (Sandbox Code Playgroud)

全文还包括web.config条目:

http://www.wiktorzychla.com/2012/09/forms-authentication-revisited.html