So I am brand new to the whole ASP.NET MVC 5 thing and I am creating my first mini application. I need to show the profile link only after a user has logged in. I have a profile controller and the link will redirect the user to the profile controller. Here is the code I have but unfortunately it is not working.
I am using the built-in ASPNet.Identity. I have only modified it to require an email address during signup. Here is the sample code I am using.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.Owin.Security;
using artisan.Models;
namespace artisan.Controllers
{
[Authorize]
public class AccountController : Controller
{
public AccountController()
: this(new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext())))
{
}
public AccountController(UserManager<ApplicationUser> userManager)
{
UserManager = userManager;
}
public UserManager<ApplicationUser> UserManager { get; private set; }
public ActionResult Profile()
{
return View();
}
//
// GET: /Account/Login
[AllowAnonymous]
public ActionResult Login(string returnUrl)
{
ViewBag.ReturnUrl = returnUrl;
return View();
So you are saying, after a user successfully logs in, you want to redirect them to some action located in another controller called ProfileController
?
If that's what you're after than it's pretty simple. After you authenticated the user in your login method you simply throw a return RedirectToAction("Index", "Profile");
in there and you should be good to go. Here is an example that does just that. It's a little more convoluted but I commented everything so you can understand. There are more than a couple redirects in there for different reasons.
[AllowAnonymous]
[HttpPost]
public async Task<ActionResult> Login(Models.AccountViewModel vm, string returnUrl)
{
//first make sure they filled in all mandatory fields
if (ModelState.IsValid)
{
//try to find the user by the credentials they provided
var user = await UserManager.FindAsync(vm.LoginModel.Username, vm.LoginModel.Password);
//if user is null then they entered wrong credentials
if (user != null)
{
//if user has confirmed their email already
if (user.EmailConfirmed == true)
{
//attempt to sign in the user
await SignInAsync(user, vm.LoginModel.RememberMe);
//if the return url is empty then they clicked directly on login instead of trying to access
//an unauthorized area of the site which redirected them to the login.
if (!string.IsNullOrEmpty(returnUrl))
return RedirectToLocal(returnUrl);
//returnUrl was empty so user went to log in first
else
{
//lets check and see which roles this user is in so we can direct him to the right page
var rolesForUser = UserManager.GetRoles(user.Id);
//users can be in multiple roles but the first role dictates what they see after they sign in
switch (rolesForUser.First())
{
case "Normal_User":
return RedirectToAction("Feed", "Account");
default:
//user is not in any roles send him to the default screen
break;
}
}
}
//user has not confirmed their email address redirect to email confirmation
else
{
//resend confirmation
await SendConfirmationEmail(user.Id);
//redirect user to unconfirmed email account view
return RedirectToAction("UnconfirmedAccount", "Account", new { Email = user.Email, UserId = user.Id });
}
}
else
{
//add errors to the view letting the user know they entered wrong credentials. Code will fall through and return
//the view below with these errors
ModelState.AddModelError("", "Invalid username or password.");
}
}
// If we got this far then validation failed
return View(vm);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.