//------------------------------------------------------------------------------
//
// Copyright (c) Telligent Systems Corporation. All rights reserved.
//
//------------------------------------------------------------------------------
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
{
///
/// This page will allow for manual creation of the Forums' Principal. It also
/// can auto-register users if they do not exist.
///
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);
}
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
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
}
}