使用自动身份验证将登录用户重定向到新网站



我有一个用ASP.NET MVC 5设计的网站,网站名称为www.oldsite.com

我们刚刚启动了一个新网站www.newsite.com,对ASP.NET MVC代码进行了一些更改,但两个网站的数据库是相同的。

当用户登录到旧网站时,www.oldsite.com验证登录详细信息(用户ID和密码(,成功登录后,根据某些条件将用户重定向到新网站www.newsite.com,并自动登录(用户无需在www.newsite.com上的登录页面中再次输入用户ID和密码(,并显示www.newsite.com的主页。

这是我的代码

int timeout = login.RememberMe ? 600 : 60; // 525600 min = 1 year
var ticket = new FormsAuthenticationTicket(v.PEmailId, login.RememberMe, timeout);
string encrypted = FormsAuthentication.Encrypt(ticket);
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encrypted);
cookie.Expires = DateTime.Now.AddMinutes(timeout);
cookie.HttpOnly = true;                        
Response.Cookies.Add(cookie);

if (some condition)
{
return Redirect("www.newsite.com");
}

我需要一些登录验证cookie代码,我使用的是ASP.NET身份。

请告诉我如何使用登录凭据(在登录页面中提供类似的userid和密码参数并自动登录到新网站(从旧网站重定向到新网站www.newsite.com,或者如何在不输入userid和口令的情况下为新网站www.newsite.com创建cookie以自动登录。

谢谢

从旧站点,您可以将用户名和密码作为参数进行传递。

return Redirect(string.Format("https://newsite.com/Home/Index?username={0}&password={1}","username", "password"));

在新网站中创建一个允许匿名用户使用的功能。然后验证用户凭据。如果用户是有效的,则添加cookie并重定向到您想要的页面。

[AllowAnonymous]
public class HomeController : Controller
{
public ActionResult Index(string username, string password)
{
// validate the user credential
// add cookie
User user = new User();
user.UserName = username;
user.Password = password;
AddCookie(user);
return RedirectToAction("Index", "Dashboard");
}
public void AddCookie(User user)
{
string encryptTicket, userData;
HttpCookie httpCookie = null;
try
{
userData = JsonConvert.SerializeObject(user);
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1, user.UserName, DateTime.Now, DateTime.Now.AddHours(1), true, userData, FormsAuthentication.FormsCookiePath);
encryptTicket = FormsAuthentication.Encrypt(ticket);
httpCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptTicket);
Response.Cookies.Add(httpCookie);
}
catch (Exception exception)
{
}
return httpCookie;
}
}
public class User
{
public string UserName { get; set; }
public string Password { get; set; }
}

最新更新