Asp.Net Forums
首页 搜索 用户列表 FAQ 注册 登录  
Asp.Net Forums » .Net 专区 » 英文原稿 » tianhong
  tianhong
帖子发起人: venjiang   发起时间: 2006-03-25 10:11 PM   回复数: 2
« 上一主题 下一主题 »
楼主
  2006-03-25, 10:11 PM
venjiang 离线,最后访问时间: 12/2/2008 9:34:33 PM venjiang



发帖数前10位

超级管理员
职务: 超级管理员
参谋长
等级: 参谋长
注册: 2004年6月8日
区域: 中国河北
积分: 2,108
精华: 9
发贴: 1,497

ASP NET Forums2.0 中文版开发团队
  Single sign-on with Forms Authentication
 

Single sign-on with Forms Authentication

Note: this entry has moved.

(Note: Updates after first post are in red - Dec 2004)

Every now and then I see people asking for some way to achieve single sign-on using Forms Authentication so you may reuse the Forms ticket with along several sites. It happens that you can have this functionality (not provided out of the box) with just a few tweaks.

Downloads

You can download the code sample for the SSO Forms Authentication from here.  The example code is provided as source code that you can use "as is" or customize it for your own applications.

SSO Sample

The sample that you can download form the above link, has two sites. The one named “FormsAuth2” is the entry point site that will call the login page located on the “FormsAuth” site. After the authentication process, the Forms ticket will be reused from the first site “FormsAuth2” with all the user name and roles info inside it. After diving into the details, let me say that these two sites are structured in two areas (public and private) in order to clearly differentiate between the publicly accessible areas and restricted areas that require authenticated access and

Secure Sockets Layer (SSL). I use separate subfolders beneath the virtual root folder of both applications to hold restricted pages such as the login form and other sample form with checkout links and the like, that needs to be secured by using HTTPS. By doing so, I can use HTTPS for specific pages without incurring the SSL performance overhead across the entire site.

Configuration

The configuration showed on the following figure is a sample of how you can set the Forms Authentication attributes with security in mind. You should follow these hints for SSO Forms Auth. First of all, you should have the same settings (see forms element attributes) that are listed below on every site that you want to adhere to SSO.

·         Name

·         Protection

·         Path

The machineKey element might be configured on the machine.config file or on every web.config application file. In the first scenario, you may have the encryption key set to something like this (this is the default setting, albeit useless for this scenario):

 

    <machineKey

        validationKey="AutoGenerate,IsolateApps"

        decryptionKey= "AutoGenerate,IsolateApps"

        validation="SHA1"/>

 

 

The "IsolateApps" means that a different key will be AutoGenerated for *each* application. You can either remove the isolateApps option (for apps on the same machine) or insert a specific key value for it to use (for apps on different boxes). This last option is the one that is used on following the config sample.

 

<configuration>

   

  <system.web>

 

    <authentication mode="Forms">

            <forms loginUrl="Secure\login.aspx"

                  protection="All"             

                  requireSSL="true"            

                  timeout="10"                 

                  name="FormsAuthCookie"         

                  path="/FormsAuth"            

                  slidingExpiration="true" />   

    </authentication>

 

    <!-- The virtual directory root folder contains general pages.

          Unauthenticated users can view them and they do not need

          to be secured with SSL. -->

    <authorization>

        <allow users="*" /> <!-- Allow all users -->

    </authorization>

 

    <machineKey

        validationKey="C50B…CABE"

        decryptionKey= "8A9BE8FD67AF6979E7D20198CFEA50DD3D3799C77AF2B72F"

        validation="SHA1"/>

 

  </system.web>

 

  <!-- The restricted folder is for authenticated and SSL access only.

       All pages on the Secure subfolder will be under SSL access. -->

  <location path="Secure" >

      <system.web>

            <authorization>

            <deny users="?" />

            </authorization>

      </system.web>

  </location>

  

</configuration>

 

 

Note: Check ouy the path attribute. This should be aligned with the app name. If you want to have SSO on every app, just leave the default value "/".

Principal Creation

After gathering the user credentials you will perform the authentication process and after that you will retrieve the user roles if you want to use the .NET role authorization pattern. This implies the creation of an Identity and a Principal object that will contain this data. So on the login page server side and after the auth process you will get the Forms ticket and save there your roles info and may be any other user profile related data (beware of size constrains, less than 4KB).

 

 

  // Do auth with your preferred auth method

  WindowsIdentity identity = WinAccessHelper.LogonUser( UserId, Password );

 

  // Add roles

  string[] roles = WinAccessHelper.Roles( new WindowsPrincipal( identity ) );

                 

  HttpCookie cookie = FormsAuthentication.GetAuthCookie( UserId.Text, false );

  FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookie.Value);

 

  // Store roles inside the Forms cookie.

  FormsAuthenticationTicket newticket = new FormsAuthenticationTicket(                                                              ticket.Version,                                                               ticket.Name,                                                                  ticket.IssueDate,                                                             ticket.Expiration,                                                            ticket.IsPersistent,                                                          String.Join( "|", roles),

                                                      ticket.CookiePath);                                                                                                  

  cookie.Value = FormsAuthentication.Encrypt(newticket);

  Context.Response.Cookies.Set(cookie);

  Response.Redirect( FormsAuthentication.GetRedirectUrl( newticket.Name, newticket.IsPersistent ) );

                 

// For different domains, should use the cookie domain

//HttpCookie formsCookie = FormsAuthentication.GetAuthCookie( UserId.Text, false );

//formsCookie.Domain = "localhost.com";

//Response.AppendCookie( formsCookie );

//Response.Redirect( FormsAuthentication.GetRedirectUrl( UserId.Text, false ) );

//FormsAuthentication.RedirectFromLoginPage( UserId.Text, false );

 

 

Principal Retrieving

On each AuthenticateRequest event of every SSO “federated” site you may retrieve your saved user info and create your Principal object and load them onto the User object of the current HttpContext instance. This is accomplished on the following figure.

 

 

protected void Application_AuthenticateRequest(Object sender, EventArgs e)

{

      if (Context.Request.IsAuthenticated)

      {

            // retrieve user's identity from httpcontext user

            FormsIdentity ident = (FormsIdentity)Context.User.Identity;

 

            // retrieve roles from the authentication ticket userdata field

            string[] arrRoles = ident.Ticket.UserData.Split(new char[] {'|'});

 

            // create principal and attach to user

            Context.User = new System.Security.Principal.GenericPrincipal(ident, arrRoles);

      }

}

 

 

Multiple Domain Scenarios

For Domain wide authentication scenarios, you can set domain-wide cookie only for second level domain, or for third level domain if second level domain contains three or less characters. It means that you cannot set cookie for domain "com" or "co.uk", but can for "example.com" or "example.co.uk". You can find a good example of this here.

Hopefully this sample will give you a good idea of how to implement a SSO scenario with Forms Authentication. Enjoy it!

 

This posting is provided "AS IS" with no warranties, and confers no rights.

 


IP 地址: 已登录   来自: 已登录    返回顶部
第 2 楼
  2006-09-16, 04:48 PM
嘿嘿哈哈 离线,最后访问时间: 9/16/2006 3:47:00 PM 嘿嘿哈哈



无等级

士兵
等级: 士兵
注册: 2006年9月16日
积分: 3
精华: 0
发贴: 3
Re: Single sign-on with Forms Authentication
 
有难度!
IP 地址: 已登录   来自: 已登录    返回顶部
第 3 楼
  2008-08-26, 09:20 AM
tianhong 离线,最后访问时间: 8/26/2008 9:23:00 AM tianhong

无等级

士兵
等级: 士兵
注册: 2008年8月26日
积分: 8
精华: 0
发贴: 9
tianhong
 
天虹上海翻译公司是国内数一数二的上海翻译公司信誉良好的上海翻译公司急你所急,专业的上海翻译公司
先祝大家新年快乐!!2008年工作顺利!!我公司招聘翻译长年有效招聘专兼职翻译:招聘翻译,专业翻译服务:翻译网址上海翻译公司翻译公司英语翻译日语翻译韩语翻译英语口译法语翻译德语翻译俄语翻译。天虹翻译公司服务项目:日语翻译英语翻译韩语翻译德语翻译法语翻译俄语翻译意大利语翻译西班牙语翻译阿拉伯语翻译葡萄牙语翻译翻译价格英文翻译中文中文翻译日文日文翻译中文英语口译同声传译等。翻译交流可进入翻译论坛,欢迎发帖。
天虹上海翻译公司,专业高质量翻译服务.欢迎咨询,公司网址:http://www.tianhongsh.com

IP 地址: 已登录   来自: 已登录    返回顶部
 第 1 页 总共 1 页 [共有 3 条记录]
Asp.Net Forums » .Net 专区 » 英文原稿 » tianhong

友情链接: hiDotNet官方论坛 | hiDotNet知识库 | 其它友情链接

Asp.Net Forums version: 2.5.2725
(C)Copyright 2004-2007, hiDotNet.com. All Rights Reserved.
意见反馈 | 关于我们

Powered by Community Server :: Forums 中文本地化: hiDotNet.com