什么FormsAuthentication.SetAuthCookie做 [英] What does FormsAuthentication.SetAuthCookie do

查看:210
本文介绍了什么FormsAuthentication.SetAuthCookie做的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用CreateUserWizard控件。在CreatedUser事件,我把这个code将用户添加到角色。

I'm using A createuserwizard control. On the CreatedUser Event I placed this code to add the user to a role.

    protected void RegisterUser_CreatedUser(object sender, EventArgs e)
    {
        FormsAuthentication.SetAuthCookie(RegisterUser.UserName, false /* createPersistentCookie */);


        if (!Roles.IsUserInRole("Test"))
        {
            var User= Membership.GetUser();
            Roles.AddUserToRole(User.UserName, "Test");
        }

        string continueUrl = RegisterUser.ContinueDestinationPageUrl;

        if (String.IsNullOrEmpty(continueUrl))
        {
            continueUrl = "~/";
        }
        Response.Redirect(continueUrl);
    }

我也想知道是什么 FormsAuthentication.SetAuthCookie(RegisterUser.UserName,假/ * * createPersistentCookie /); 表示,什么它的用途以及为什么 Membership.GetUser()为null。

I would also like to know what FormsAuthentication.SetAuthCookie(RegisterUser.UserName, false /* createPersistentCookie */); means and what it's used for and why Membership.GetUser() is null.

推荐答案

你有 LoginCreatedUser =FALSE DisableCreatedUser =真 的CreateUserWizard

这些将prevent用户从被记录在马上,并引起 Membership.GetUser()来返回null,因为用户当前没有登录

Those will prevent the user from being logged in immediately, and cause Membership.GetUser() to return null, because the user isn't currently logged in.

如果你想马上登录的用户,设置既不或两者 LoginCreatedUser =真正的 DisableCreatedUser =FALSE的CreateUserWizard 。这应该让你的当前code工作。

If you want the user to be logged in immediately, set neither or both LoginCreatedUser="true" and DisableCreatedUser="false" on your CreateUserWizard. That should get your current code working.

FormsAuthentication.SetAuthCookie()设置浏览器cookie来启动用户会话。这是什么让每个登录页面发布到服务器时的用户。 createPersistentCookie 创建一个永久性的Cookie在浏览器关闭时,不到期,因此用户可以返回网站并自动登录。应该根据用户是否选中了记住我复选框,你的登录表单上。它不是默认情况下可用的的CreateUserWizard 形式,但你可以在你的模板中添加一个复选框它,如果你喜欢。

FormsAuthentication.SetAuthCookie() sets a browser cookie to initiate the user's session. It's what keeps the user logged in each time a page is posted to the server. createPersistentCookie creates a persistent cookie that doesn't expire when the browser is closed, so the user can return to the site and be logged in automatically. It should be based on whether the user checked the "Remember me" checkbox on your Login form. It isn't available on the CreateUserWizard form by default, but you can add a checkbox for it in your template, if you like.

如果您不想让用户自动登录,从code删除 FormsAuthentication.SetAuthCookie()线,并设置的CreateUserWizard 适当的属性。如果要批准的用户,他们可以登录之前,请将 DisableCreatedUser =真正的。从你之前设定的用户身份登录,将prevent它们 IsApproved = TRUE 从无论是在IIS管理器中的.NET用户模块,或者你自己的自定义网页批准的用户。

If you don't want to have the user logged in automatically, remove the FormsAuthentication.SetAuthCookie() line from your code, and set the CreateUserWizard properties appropriately. If you want to approve users before they can log in, set DisableCreatedUser="true". That will prevent them from logging in until you set the user IsApproved=true from either the .Net Users module in the IIS Manager, or you own custom web page to approve users.

您仍然可以设置用户添加到适当的角色创建用户时无需登录他们:

You can still set add the user to the appropriate role when the user is created without needing to log them in:

if (!Roles.IsUserInRole(RegisterUser.UserName, "Test"))
{  
    Roles.AddUserToRole(RegisterUser.UserName, "Test");
}

这篇关于什么FormsAuthentication.SetAuthCookie做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆