我可以在自定义 MembershipProvider 中的用户会话中添加一些内容吗?
我正在实现一个自定义 MembershipProvider,以便将登录详细信息传递给我们在公司其他几个地方使用的自定义业务对象。 但是,一旦我们进行了身份验证,我想将这个初始化的业务对象保存在会话中,以便稍后在其他页面中使用。 让我举个例子。
public override bool ValidateUser(string username,string password)
{
try
{
// I want to keep this "object" in the Session to be used later on
CustomBusinessObject object = new CustomBusinessObject(username, password);
return true;
}
catch (CustomBusinessAuthenticationException)
{
return false;
}
}
我有办法做到这一点吗? 我没有立即找到通过实现此自定义 MembershipProvider 来访问 Session 对象的方法。
I am implementing a custom MembershipProvider in order to pass the login details to a custom business object that we use in several other places in our company. But once we have authenticated I'd like to save this initialized business object in the session to be used later in other pages. Let me give an example.
public override bool ValidateUser(string username,string password)
{
try
{
// I want to keep this "object" in the Session to be used later on
CustomBusinessObject object = new CustomBusinessObject(username, password);
return true;
}
catch (CustomBusinessAuthenticationException)
{
return false;
}
}
Is there a way for me to do this? I didn't immediately see a way to get access to the Session object through implementing this custom MembershipProvider.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过调用 System.Web.HttpContext.Current 来访问会话。 只需在自定义成员资格提供程序上创建一个自定义属性,检查 HttpContext.Current 是否为 null,如果是,则返回 null,否则相应地访问会话值。
You can access the session by calling System.Web.HttpContext.Current. Just create a custom property on your custom membership provider that checks to see if HttpContext.Current is null and if so, returns null, otherwise access the session value accordingly.
您应该能够访问 System.Web.HttpContext.Current.Session 。
请记住,如果您的提供程序曾经在 ASP.Net 管道之外使用过,则
System.Web.HttpContext
将为null
,并且通过在您的提供程序内部使用它,您将将您的提供商与 ASP.Net 紧密耦合。You should be able to access
System.Web.HttpContext.Current.Session
.Keep in mind that
System.Web.HttpContext
will benull
if your provider is ever used outside of the ASP.Net pipeline, and that by using it inside your provider you will be coupling your provider tightly to ASP.Net.