asp.net - 在 asp.net 中添加对表单例份验证的声明

标签 asp.net asp.net-mvc

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

FormsAuthentication.SetAuthCookie(user,true)  

而且我还想添加我从 windows azure 表存储中获得的用户声明,以便我以后可以使用类似的方法阅读它们
ClaimsPrincipal claimsPrincipal = Page.User as ClaimsPrincipal; 

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

谢谢

最佳答案

在 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"] );

全文还包括 web.config条目:

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

关于asp.net - 在 asp.net 中添加对表单例份验证的声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23416288/

相关文章:

asp.net-mvc - Microsoft Owin Facebook 身份验证在 MVC 应用程序中不起作用

asp.net-mvc - 如何使用 ASP.NET Core v2 在 Visual Studio 2017 中安装 font-awesome

c# - 如何在 PlaceHolder 上使用工具提示?

c# - 如何最好地在 ASP.NET Core 中实现 Google 社交登录身份验证?

javascript - 如何将两个盒子自动求和到另一个盒子中?

asp.net-mvc - 在客户端清除outputcache

亚马逊云上的 ASP.NET 网站

asp.net - cType : string to date throwing InvalidCastException (ASP) (VB. NET)

c# - 压缩完成后立即删除图像文件。如何?

c# - 由于设计不当的业务和数据库层,MVC4 中的极端性能问题