⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 authorizationredirect.aspx.cs

📁 本系统是在asp版《在线文件管理器》的基础上设计制作
💻 CS
字号:
//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Security.Principal;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Diagnostics;
using AspNetForums.Components;

namespace AspNetForums
{
	/// <summary>
	/// This page will allow for manual creation of the Forums' Principal.  It also
	/// can auto-register users if they do not exist.
	/// </summary>
	public class AuthorizationRedirect : System.Web.UI.Page
	{
		private void Page_Load(object sender, System.EventArgs e)
		{


            //
			// Insert all Cookie related code here...
            //


            // Set username to your cookie's username.
            string username = "TestUser9";

            // Lookup and/or create username.
            //
            LookupUser(username);    // the forums could have differnet case

            // Now login the user if we didn't have an error.
            //
            LoginUser(username);

            // Redirect
            //
            Response.Redirect("/Forums/");
            
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

        #region LookupUser
        private string LookupUser (string username) 
        {
            // attempt to lookup the username
            //
            AspNetForums.Components.User tmpUser = AspNetForums.Users.FindUserByUsername(username);

            if (tmpUser.IsAnonymous) 
            {
                // user was not found, create one
                //

                tmpUser.Username = username;        // REQUIRED
                tmpUser.Email = username + "@doesnotexist.com";     // REQUIRED
                tmpUser.Password = "CustomCreation_" + DateTime.Now.ToLongTimeString();;  // REQUIRED
                tmpUser.IsAnonymous = false;  // REQUIRED

                // attempt to create the user
                AspNetForums.Enumerations.CreateUserStatus status = AspNetForums.Users.Create(tmpUser, false);

                // check for any errors
                if (status != AspNetForums.Enumerations.CreateUserStatus.Created) 
                {

                    // you can put some error logging/capturing here to break out of this procedure:
                    //
                    //      AspNetForums.Enumerations.CreateUserStatus.DisallowedUsername
                    //      AspNetForums.Enumerations.CreateUserStatus.DuplicateEmailAddress
                    //      AspNetForums.Enumerations.CreateUserStatus.DuplicateUsername
                    //      AspNetForums.Enumerations.CreateUserStatus.InvalidFirstCharacter
                    //      AspNetForums.Enumerations.CreateUserStatus.UnknownFailure
                    //
                    return null;

                } 
            }

            // We should now have a valid User object.
            //
            return tmpUser.Username.ToString();
        }
        #endregion

        #region LoginUser
        private void LoginUser (string username) 
        {

            // Create the authentication ticket
            //
            FormsAuthenticationTicket authTicket = new FormsAuthenticationTicket( 1,                          // version
                username,           // user name
                DateTime.Now,               // creation
                DateTime.Now.AddMinutes(60),// Expiration
                true,                      // Persistent
                "Everyone" );                    // 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); 

        }
        #endregion
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -