我有一个在MVC 2.0中开发的Web应用程序。我为这个Web应用程序创建了一个动态数据网站。为了访问动态数据网站,用户被重定向到MVC应用程序的登录页面。在同一个IIS服务器上的两个Web应用程序之间共享会话
登录时,将为用户创建会话。我想在我的动态数据网站上访问此会话以检查用户的角色和用户名。如果用户具有管理员角色,则只允许用户访问动态数据网站。
我该如何做到这一点?
我有一个在MVC 2.0中开发的Web应用程序。我为这个Web应用程序创建了一个动态数据网站。为了访问动态数据网站,用户被重定向到MVC应用程序的登录页面。在同一个IIS服务器上的两个Web应用程序之间共享会话
登录时,将为用户创建会话。我想在我的动态数据网站上访问此会话以检查用户的角色和用户名。如果用户具有管理员角色,则只允许用户访问动态数据网站。
我该如何做到这一点?
尝试这种解决方案:
你的第一个Web应用程序(即用户用于登录的一个)
//create a method that will redirect to your web app 2
public ActionResult RedirectToAnotherSite(string name, string role)
{
//specify the IP or url for your web app 2
//pass the parameters
var rawUrl = string.Format("http://localhost:8051?name={0}&role={1}", name, role);
return Redirect(rawUrl);
}
在你的web应用程序2(动态)打开文件Global.asax中并添加以下代码
void Application_BeginRequest(object sender, EventArgs e)
{
//get the passed parameters
var req = ((HttpApplication)(sender)).Request;
var name = req.QueryString["name"] ?? string.Empty;
var role = req.QueryString["role"] ?? string.Empty;
//check the role
if (!role.Equals("admin"))
{
//return http access denied
var res = ((HttpApplication)(sender)).Response;
res.StatusCode = 401;
}
}
感谢亲爱这真的有帮助。但是,您能告诉我如何使用查询字符串的加密和解密为安全原因 – Suri
我会稍后分享它 –
“该会话是为用户创建的”---您正在存储的会话细节是什么? –
用户的登录名和角色。 – Suri
它是我很好地工作: http://stackoverflow.com/questions/2868316/sharing-sessions-across-applications-using-the-asp-net-session-state-service –