ASP.net MVC 域范围内单点登录(一)
站点1:bss.cds.com
站点2:oss .cds.com
要求:无论在哪个站点登录,访问另一个站的时候都不需要再登录。
使用sso方案太过庞大,简洁方案如下:
1、修改web.config
<authentication mode="Forms">
<forms domain=".cds.com" loginUrl="~/Account/Login" timeout="2880" />
</authentication>
2、mvc部分:
if (User.Identity.Name == "")
{
//下面是伪码
bool checked = Login( function { get username password });
if checked
{
//下面是伪码
bool checked = Login( function { get username password });
if checked
{
FormsAuthentication.SetAuthCookie( username , false);
}
else
return " 登录失败 ";
else
return " 登录失败 ";
}
经测试,该方案成立。
测试部分:
1、修改/windows/system32/drivers/etc/hosts ,添加:
127.0.0.1 bss.cds.com
127.0.0.1 oss.cds.com
127.0.0.1 www.test.com
2、生成简单的 mvc4 程序,修改 Web.config
<authentication mode="Forms">
<forms domain=".cds.com" loginUrl="~/Account/Login" timeout="2880" />
</authentication>
3、修改 iis express 配置:(/我的文档/iisexpress/appliactionhost.config )
找到对应的程序,进行修改,将蓝色的host改为 *:
<site name="MvcApplication1(6)" id="26">
<application path="/" applicationPool="Clr4IntegratedAppPool">
<virtualDirectory path="/" physicalPath="C:\AutoDeploy4.5\test\MvcApplication1\MvcApplication1" />
</application>
<bindings>
<binding protocol="http" bindingInformation="*:26181:localhost" />
</bindings>
</site>
4、以管理员身份启动 visualstudio。
5、程序如下:
6、编译、运行、设置断点。
7、
a、在浏览器里打开: http://bss.cds.com:26181, 通过断点能看到,首次执行时User.Identity.Name 无值, 蓝色代码被执行。
b、接着在浏览器里打开 :http://oss.cds.com:26181, 通过断点能看到,首次打开的时候,User.Identity.Name 为 “Roger_1”, 蓝色代码未被执行。
c、 接着在浏览器里打开:http://www.test.com:2681, 通过断点来看,首次执行时User.Identity.Name 无值, 蓝色代码被执行。
4、以管理员身份启动 visualstudio。
5、程序如下:
public ActionResult Index()
{
ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
if (User.Identity.Name == "")
{
FormsAuthentication.SetAuthCookie("Roger_1", false);
ViewBag.SetUser = "是";
}
else
ViewBag.SetUser = "否";
ViewBag.CurrentUser = User.Identity.Name;
return View();
}6、编译、运行、设置断点。
7、
a、在浏览器里打开: http://bss.cds.com:26181, 通过断点能看到,首次执行时User.Identity.Name 无值, 蓝色代码被执行。
b、接着在浏览器里打开 :http://oss.cds.com:26181, 通过断点能看到,首次打开的时候,User.Identity.Name 为 “Roger_1”, 蓝色代码未被执行。
c、 接着在浏览器里打开:http://www.test.com:2681, 通过断点来看,首次执行时User.Identity.Name 无值, 蓝色代码被执行。
证明跨子域登录成功, 而且不会出主域。

浙公网安备 33010602011771号