退出声明身份验证

时间:2015-04-19 01:12:44

标签: c# forms-authentication claims-based-identity asp.net-mvc-5.1

我已成功在我的项目中实施了声明身份验证。

如下所示:

 var userCredentials = new[] {
                new Claim("UserId", userProfile.UserId.ToString()),
                new Claim("Username", userProfile.UserName)};

        var id = new ClaimsIdentity(userCredentials, "Forms");

        var cp = new ClaimsPrincipal(id);
        var token = new SessionSecurityToken(cp);

        var sam = FederatedAuthentication.SessionAuthenticationModule;
        sam.WriteSessionTokenToCookie(token);

哪个工作正常,我试图解决的问题是签署该用户,我有以下类,当用户按下注销时调用

 public static void SignOut()
 {
     FormsAuthentication.SignOut();
 }

似乎没有将用户注销,所以我进行了谷歌搜索并尝试了以下内容:

FederatedAuthentication.SessionAuthenticationModule.SignOut();
FederatedAuthentication.SessionAuthenticationModule.DeleteSessionTokenCookie();

他们俩都没有工作?我可能做错了什么?

这是我的配置:

 <configSections>
     <section name="system.identityModel" type="System.IdentityModel.Configuration.SystemIdentityModelSection, System.IdentityModel, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
     <section name="system.identityModel.services" type="System.IdentityModel.Services.Configuration.SystemIdentityModelServicesSection, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
</configSections>

<system.web>
    <authentication mode="Forms">
       <forms loginUrl="/User/Login" timeout="2880" />
    </authentication>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" maxRequestLength="1048576" />
</system.web>

 <modules>
  <add name="SessionAuthenticationModule" type="System.IdentityModel.Services.SessionAuthenticationModule, System.IdentityModel.Services, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</modules>

任何帮助都将不胜感激。

更新

我刚试过以下内容:

FormsAuthentication.SignOut();
Session.Abandon();

// clear authentication cookie
HttpCookie cookie1 = new HttpCookie(FormsAuthentication.FormsCookieName, "");
cookie1.Expires = DateTime.Now.AddYears(-1);
Response.Cookies.Add(cookie1);

// clear session cookie (not necessary for your current problem but i would recommend you do it anyway)
HttpCookie cookie2 = new HttpCookie("ASP.NET_SessionId", "");
cookie2.Expires = DateTime.Now.AddYears(-1);
Response.Cookies.Add(cookie2);

并重定向到常见问题解答页面,但我仍然可以看到导航中的链接,只有在用户经过身份验证时才会显示这些链接。

1 个答案:

答案 0 :(得分:2)

管理让这个工作!!

我现在用

var sam = FederatedAuthentication.SessionAuthenticationModule;
sam.DeleteSessionTokenCookie();

然后我进行重定向,它按预期工作:)