Nom*_*Nom 4 .net c# asp.net-4.5
我正在构建一个应用程序并将其与活动目录集成.
因此,我将我的应用程序用户验证为活动目录用户,之后我将一些用户数据存储为:user group and user profile将通用主体和用户身份存储到通用身份.
我的问题是,当我想使用它时,我无法从通用主体获取用户配置文件数据.
有人可以告诉我该怎么做吗?
string cookieName = FormsAuthentication.FormsCookieName;
HttpCookie authCookie = Context.Request.Cookies[cookieName];
if (authCookie == null)
{
//There is no authentication cookie.
return;
}
FormsAuthenticationTicket authTicket = null;
try
{
authTicket = FormsAuthentication.Decrypt(authCookie.Value);
}
catch (Exception ex)
{
//Write the exception to the Event Log.
return;
}
if (authTicket == null)
{
//Cookie failed to decrypt.
return;
}
String data = authTicket.UserData.Substring(0, authTicket.UserData.Length -1);
string[] userProfileData = data.Split(new char[] { '|' });
//Create an Identity.
GenericIdentity id =
new GenericIdentity(authTicket.Name, "LdapAuthentication");
//This principal flows throughout the request.
GenericPrincipal principal = new GenericPrincipal(id, userProfileData);
Context.User = principal;
Run Code Online (Sandbox Code Playgroud)
注意:上面的代码在全局asax文件中,我想使用我在通用主体中存储的用户配置文件数据在另一个名为的文件中default.aspx.
gid*_*eon 10
所以首先你不应该这样做:
GenericPrincipal principal = new GenericPrincipal(id, userProfileData);
//^^ this is wrong!!
Run Code Online (Sandbox Code Playgroud)
构造函数的第二个参数是Roles.查看文档.
如果要将数据存储到通用主体中,那么您应该做的是
创建一个类GenericIdentity:
class MyCustomIdentity : GenericIdentity
{
public string[] UserData { get; set;}
public MyCustomIdentity(string a, string b) : base(a,b)
{
}
}
Run Code Online (Sandbox Code Playgroud)像这样创建它:
MyCustomIdentity =
new MyCustomIdentity(authTicket.Name,"LdapAuthentication");
//fill the roles correctly.
GenericPrincipal principal = new GenericPrincipal(id, new string[] {});
Run Code Online (Sandbox Code Playgroud)在这样的页面中获取它:
所以例如在Page load中你可以这样做:
protected void Page_Load(object sender, EventArgs e) {
MyCustomIdentity id = (MyCustomIdentity)this.User.Identity
var iWantUserData = id.UserData;
}
Run Code Online (Sandbox Code Playgroud)| 归档时间: |
|
| 查看次数: |
6506 次 |
| 最近记录: |