在Asp.Net MVC 5中获取登录用户的UserID(Get UserID of logged-in user in Asp.Net MVC 5)
我对ASP.Net MVC比较陌生,现在尝试使用内置的用户登录功能。 我可以在注册视图中注册用户。 如果我尝试使用创建的用户登录,这也适用。 我被重定向到母版页。
但是我无法获取当前用户的用户标识。 我在HomeController和AccountController中尝试了我的代码,但都没有工作。 第一行中的语句总是返回null。
var userID = User.Identity.GetUserId(); if (!string.IsNullOrEmpty(userID)) { var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(ApplicationDbContext.Create())); var currentUser = manager.FindById(User.Identity.GetUserId()); }
在获取用户ID之前,我还需要其他的东西吗?
I'm relatively new to ASP.Net MVC and try to use the built-in user login functionality now. I'm able to register an user in the registration view. If I try to login with the created user this also works. I'm redirected to the master page.
But I'm not able to get the UserID of the current user. I tried my code in the HomeController and in the AccountController, but both didn't work. The statement in the first line returns always null.
var userID = User.Identity.GetUserId(); if (!string.IsNullOrEmpty(userID)) { var manager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(ApplicationDbContext.Create())); var currentUser = manager.FindById(User.Identity.GetUserId()); }
Do I have to to something else before getting the UserID?
原文:https://stackoverflow.com/questions/26739778
最满意答案
是的,你可以使用momentjs解析它并将其与字符串进行比较
function isValidDate(str) { var d = moment(str,'D/M/YYYY'); if(d == null || !d.isValid()) return false; return str.indexOf(d.format('D/M/YYYY')) >= 0 || str.indexOf(d.format('DD/MM/YYYY')) >= 0 || str.indexOf(d.format('D/M/YY')) >= 0 || str.indexOf(d.format('DD/MM/YY')) >= 0; }
测试代码
tests = ['',' ','x','1/1','1/12/2015','1/12/2015 1:00 PM'] for(var z in tests) { var test = tests[z]; console.log('"' + test + '" ' + isValidDate(test)); }
产量
"" false " " false "x" false "1/1" false "1/12/2015" true "1/12/2015 1:00 PM" true
Yes, you could use momentjs to parse it and compare it back with the string
function isValidDate(str) { var d = moment(str,'D/M/YYYY'); if(d == null || !d.isValid()) return false; return str.indexOf(d.format('D/M/YYYY')) >= 0 || str.indexOf(d.format('DD/MM/YYYY')) >= 0 || str.indexOf(d.format('D/M/YY')) >= 0 || str.indexOf(d.format('DD/MM/YY')) >= 0; }
Test code
tests = ['',' ','x','1/1','1/12/2015','1/12/2015 1:00 PM'] for(var z in tests) { var test = tests[z]; console.log('"' + test + '" ' + isValidDate(test)); }
Output
"" false " " false "x" false "1/1" false "1/12/2015" true "1/12/2015 1:00 PM" true
相关问答
更多-
是的,你可以使用momentjs解析它并将其与字符串进行比较 function isValidDate(str) { var d = moment(str,'D/M/YYYY'); if(d == null || !d.isValid()) return false; return str.indexOf(d.format('D/M/YYYY')) >= 0 || str.indexOf(d.format('DD/MM/YYYY')) >= 0 || str.inde ...
-
如果那是你的整个字符串,那就试着解析它: if (DateTime::createFromFormat('Y-m-d G:i:s', $myString) !== FALSE) { // it's a date } If that's your whole string, then just try parsing it: if (DateTime::createFromFormat('Y-m-d H:i:s', $myString) !== FALSE) { // it's a date }
-
require 'date' begin Date.parse("31-02-2010") rescue ArgumentError # handle invalid date end require 'date' begin Date.parse("31-02-2010") rescue ArgumentError # handle invalid date end
-
如何将时刻日期转换为字符串并删除时刻对象(How to convert moment date to a string and remove the moment object)[2021-02-06]
使用moment().format()从日期创建一个格式化的字符串。 console.log(moment().format()) 但是如果你使用2.1.0+版本( 链接 ), toString应该可以工作: console.log(moment().toString()) console.log(typeof moment() ... -
用时刻严格解析日期(Strictly parsing dates with moment)[2023-02-27]
从版本2.3.0开始,您可以为最后一个参数指定一个布尔值,以使Moment使用严格的解析。 严格的解析要求格式和输入完全匹配,包括分隔符。 moment('It is 2012-05-25', 'YYYY-MM-DD').isValid(); // true moment('It is 2012-05-25', 'YYYY-MM-DD', true).isValid(); // false 在Moment JS文档的这一部分中找到 As of version 2.3.0, you may sp ... -
对于以前版本的Moment.js来说,这是一个已知问题: YYYY-MM-DD,对于isValid(),3位数年份返回true 。 它已于2013年12月修复,您的代码按照预期使用最新版本的库。 版本2.3.1 于2013年10月发布,不包括此修复程序。 This was a known issue for past versions of Moment.js: YYYY-MM-DD with 3 digit year returns true for isValid(). It was fixed in ...
-
如何使用moment.js测试javascript对象是否为Date(How to test if a javascript object is a Date using moment.js)[2023-03-19]
您希望在解析中使用严格模式。 严格模式将使Moment仅匹配与格式字符串完全匹配的输入。 只需将true作为第三个参数传递: moment("1", "DD/MM/YYYY", true).isValid(); false moment("1/", "DD/MM/YYYY", true).isValid(); false moment("1/1", "DD/MM/YYYY", true).isValid(); false 请注意,在严格模式下,使用DD和MM令牌,Moment将开始预期两位数的日期和月份。 ... -
如何测试日期格式字符串是否是Oracle中的有效日期格式字符串(How to test if date format string is a valid date format string in Oracle)[2024-02-28]
你可以创建一个函数: 例如: FUNCTION is_valid_date_format ( p_format IN VARCHAR2 ) RETURN BOOLEAN IS l_date VARCHAR2(100) := NULL; BEGIN l_date := TO_char( sysdate, p_format ); RETURN TRUE; EXCEPTION WHEN OTHERS THEN RETURN FALSE; END is_v ... -
时刻格式字符串无法识别(Moment format string not recognized)[2023-01-01]
使用YYYY代替yyyy 。 根据时刻格式文档 ,年份的有效格式是YY (示例输出"15" )和YYYY (示例输出"2015" )。 未被识别为格式标记的字符包含在文字中。 Use YYYY instead of yyyy. According to the moment format docs the valid formats for year are YY (example output "15") and YYYY (example output "2015"). Characters that ... -
你可以做这样的事情 moment('2016-10-03T00:00:00.000Z', 'YYYY-MM-DD').isValid(); 要检查完整格式,我想你可以做这样的事情 moment('2016-10-03T00:00:00.000Z', 'YYYY-MM-DD HH:mm:ms').isValid(); You can do something like this moment('2016-10-03T00:00:00.000Z', 'YYYY-MM-DD').isValid(); T ...