我需要有关应用程序问题的帮助。使用MVC 4(newdev) [英] I need help with an application issue. Using MVC 4 (newdev)
问题描述
登录模型
Login Model
namespace jury_service.Models
{
public class LoginModel
{
[Display(Name = "Username")]
[Required]
public string username { get; set; }
[Display(Name = "Password")]
[Required]
public string Password { get; set; }
//protected readonly WebClient Client;
//public LoginModel(WebClient client)
//{
// Client = client;
//}
public FCC_Users infoModel;
public FCC_Users info;
public List<FCC_Users> infos;
public bool DoLogin(LoginModel login)
{
var ffcuser = GetInfoREcordByName(login.username);
//Check if user model pulled back from API is null
if (ffcuser != null)
{
//if not null check to see if it has a password/intial value of '1'...
if (!String.IsNullOrEmpty(ffcuser.Secret))
{
if (ffcuser.Secret == login.Password)
{
//Check if user login is initial login...
if (ffcuser.Initial.Equals(1))
{
return true;
}
}
}
else
{
return false;
}
}
return false;
}
//public bool DoLogin()
//{
// if (Initial == "1" && Password != "null")
// {
// return true;
// }
// return false;
//}
//public User GetUser(LoginModel login)
//{
// try
// {
// //Add Request Headers
// Client.Headers.Add(HttpRequestHeader.ContentType, "application/json");
// //Make Request to API and return results
// return JsonConvert.DeserializeObject<User>(Client.DownloadString(new Uri(Properties.Settings.Default.Api_Url + "Info" + "/?username=" + login.username + "/")));
// }
// catch (Exception ex)
// {
// return null;
// }
//}
public FCC_Users GetInfoREcordByName(string infoName)
{
WebClient client = new WebClient();
string url = Properties.Settings.Default.Api_Url + "Info/?infoName=" + infoName;
info = JsonConvert.DeserializeObject<FCC_Users>(client.DownloadString(url));
return info;
}
public FCC_Users GetAllInfoRecords(string infoName)
{
WebClient client = new WebClient();
string url = Properties.Settings.Default.Api_Url + "Info";
infos = JsonConvert.DeserializeObject<List<FCC_Users>>(client.DownloadString(url));
return infos.Where(x => x.Username == infoName).First()
}
}
}
登录控制器
Login Controller
namespace jury_service.Controllers
{
public class FCCLoginController : Controller
{
//private FCC_Users username;
//
// GET: /FCCLogin/
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(LoginModel login)
{
if (ModelState.IsValid)
{
var ffcuser = login.GetAllInfoRecords(login.username);
//Check to see if user exists...
if (ffcuser != null)
{
//If user exists, check password matches...
//If password matches, check if initial login...
if (!String.IsNullOrEmpty(ffcuser.Secret))
{
if (ffcuser.Secret == login.Password)
{
//Check if user login is initial login...
if (ffcuser.Initial)
{
return RedirectToAction("CreatePassword", "Newpassword");
}
//else
// if (ffcuser != username)
{
ModelState.AddModelError("Error", "Wrong Username and/or Password");
//ModelState.Clear();
}
}
}
return RedirectToAction("fccindex", "FCCLogin");
}
}
return View();
}
public ActionResult fccindex()
{
return View();
}
我的尝试:
我尝试登录验证。如果用户名和&密码是正确的而不是错误,但如果不正确,则会在此处发生错误.First
返回infos.Where(x => x.Username == infoName)。第一个();
What I have tried:
My attempt at a login authentication. If username & password is correct not error, but if incorrect the error happens in here at .First
return infos.Where(x => x.Username == infoName).First();
推荐答案
我想因为它没有找到任何结果,所以它是null并且发生异常。
参考 - Enumerable.First(TSource)方法(IEnumerable( TSource))(System.Linq) [ ^ ]
I guess as it is not finding any result, so it is null and exception happens.
Refer - Enumerable.First(TSource) Method (IEnumerable(TSource)) (System.Linq)[^]
首先< TSource > (IEnumerable < TSource > )如果 source 不包含任何元素, 方法将引发异常。要在源序列为空时返回默认值,请使用 FirstOrDefault 方法。
The First<TSource>(IEnumerable<TSource>) method throws an exception if source contains no elements. To instead return a default value when the source sequence is empty, use the FirstOrDefault method.
你应该使用FirstOrDefault。
You should user FirstOrDefault.
删除这个
return infos.Where(x => x.Username == infoName).First()
add
add
return infos.FirstOrDefault(x => x.Username == infoName);
make确定您要验证空值。
make sure you are validating for null values.
这篇关于我需要有关应用程序问题的帮助。使用MVC 4(newdev)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!