[转贴]Forms authentication and role-based security

博客展示了使用FormsAuthentication进行用户认证的代码实现。创建认证票据并加密,将加密票据添加到Cookie中,最后将用户重定向到最初请求的页面,还给出了具体例子的参考链接。

// Create the authentication ticket

FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket(

                                    1,                          //version

                                    txtUserName.Text,           // user name

                                    DateTime.Now,               // creation

                                    DateTime.Now.AddMinutes(60),//Expiration

                                    false,                      //Persistent

                                    String.Join( "|", roles));  // User data

 

// Now encrypt the ticket.

string encryptedTicket = FormsAuthentication.Encrypt(authTicket);

// Create a cookie and add the encrypted ticket to the cookie as data.

HttpCookie authCookie = new HttpCookie(FormsAuthentication.FormsCookieName,

                                                               encryptedTicket);

 

// Add the cookie to the outgoing cookies collection.

Response.Cookies.Add(authCookie);

 

// Redirect the user to the originally requested page

Response.Redirect(FormsAuthentication.GetRedirectUrl(txtUserName.Text, false));

Compare with the proposed approach:

 

 

  // Get the cookie created by the FormsAuthentication API

  // Notice that this cookie will have all the attributes according to  

  // the ones in the config file setting.      

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

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

 

  // Store roles inside the Forms Ticket with all the attributes aligned with

  // the config Forms section.

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

                                                      ticket.CookiePath);          

  // add the encrypted ticket to the cookie as data.                                        

  cookie.Value = FormsAuthentication.Encrypt(newticket);

  // Update the outgoing cookies collection.

  Context.Response.Cookies.Set(cookie);

 

  // Redirect the user to the originally requested page

  Response.Redirect( FormsAuthentication.GetRedirectUrl( newticket.Name,

                                                     newticket.IsPersistent ) );

 

<configuration>   

  <system.web>

 

    <authentication mode="Forms">

            <forms loginUrl="Secure/login.aspx"

                  protection="All"             

                  requireSSL="true"            

                  timeout="10"                 

                  name="FormsAuthCookie"         

                  path="/FormsAuth"            

                  slidingExpiration="true" />   

    </authentication>

 

  </system.web> 

</configuration>

具体例子请参照http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnnetsec/html/SecNetHT04.asp?frame=true

 

根据提供的引用内容,可以看出问题是关于"Authentication不匹配spring-security"的。引用和引用提供了一些有关配置spring-security的代码片段,引用则提供了一些关于导包和更新整理的信息。 根据引用中的代码片段,可以看到security:authentication-manager用于配置身份验证管理器,并且security:authentication-provider用于指定用户服务的引用。在此配置中,用户服务的引用应该是一个实现了UserDetailsService接口的bean,用于提供用户信息。 引用中的代码片段提供了一个配置spring-security的一般步骤,其中pom.xml文件中的依赖项用于添加spring-security的相关库。 根据引用中的代码片段,可以看出在导包中包含了spring-security的相关依赖项,其中spring-boot-starter-security是用于启用Spring Security的starter库。 根据提供的信息,可能的原因是配置文件中的Authentication不匹配spring-security的要求,可能存在配置错误或缺失。 为了解决这个问题,你可以检查以下几个方面: 1. 确认配置文件中的authentication-manager和authentication-provider是否正确配置,并且引用了正确的用户服务。 2. 检查pom.xml文件中的依赖项是否正确添加,并且版本号是否与你使用的Spring Security版本相匹配。 3. 确保你的代码中包含了正确的导包,并且没有注释掉必要的依赖项。 希望以上信息对你有帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [spring-security 常见错误 及简单配置](https://blog.youkuaiyun.com/qq_43220949/article/details/107445437)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [spring-security【2022-3-18更新】](https://blog.youkuaiyun.com/m0_53964515/article/details/123471888)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值