Search

Saturday, April 4, 2015
0 comments

SignIn after registration in ASP.NET

11:53 PMSaturday, April 4, 2015
In my previous article, we have already learned  that how to create registration page in asp.net 4.5. Today we talk about signin after registration.  Simple API provided by the microsoft  that is Microsoft.AspNet.Identity, various methods and classes given init by the microsoft, such as UserManager class etc . After successfully registration you can  check, whether the registration is success or not using IdentityResult class. If IdentityResult class return true then you will signin into the account using static method of SignIn, which is available in IdentityHelper class. Now, code of your application is

IdentityResult result = manager.Create(user, Password.Text);

if (result.Succeeded)
{

IdentityHelper.SignIn(manager, user, isPersistent: false);

IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);

}

The Simple static SignIn method of IdentityHelper class is

public static void SignIn(UserManager manager, ApplicationUser user, bool isPersistent)
{
IAuthenticationManager authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
authenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
var identity = manager.CreateIdentity(user, DefaultAuthenticationTypes.ApplicationCookie);
authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
}

After signin you can redirect to other page. A very simple method provided by the microsoft that is RedirectToReturnUrl (static), which is also available in IdentityHelper class that is


public static void RedirectToReturnUrl(string returnUrl, HttpResponse response)
{
if (!String.IsNullOrEmpty(returnUrl) && IsLocalUrl(returnUrl))
{
response.Redirect(returnUrl);
}
else
{
response.Redirect("~/");
}
}
Protected by Copyscape Online Copyright Protection Software

0 comments :

Post a Comment

 
Toggle Footer
Top