c# - Two questions about MVC, and Identity -
i new identity , mvc, trying create mvc app 1 of first projects.
i have been able follow few tutorials , have added additional properties applicationuser : identityuser class
public class applicationuser : identityuser<string, applicationuserlogin, applicationuserrole, applicationuserclaim> { [required] [display(name = "username")] [stringlength(50)] public string handle { get; set; } [stringlength(100, errormessage = "your {0} can @ {1} characters long.")] [display(name = "first name")] public string firstname { get; set; } [stringlength(100, errormessage = "your {0} can @ {1} characters long.")] [display(name = "last name")] public string lastname { get; set; } [required] [display(name = "user creation date")] public datetime usercreationdate { get; set; } public applicationuser() { this.id = guid.newguid().tostring(); // add custom user properties/code here }
my questions are:
i see email set require unique email in app_start.identityconfig.cs, there way set require unique custom property handle?
var manager = new applicationusermanager(new userstore<applicationuser, applicationrole, string, applicationuserlogin, applicationuserrole, applicationuserclaim>(context.get<applicationdbcontext>())); // configure validation logic usernames manager.uservalidator = new uservalidator<applicationuser>(manager) { allowonlyalphanumericusernames = false, requireuniqueemail = true };
in partial view of views.shared._loginpartial.cshtml shows username/email of person logging application using user.identity.getusername() there way reference 1 of custom properties there instead such firstname, or handle?
@using microsoft.aspnet.identity @if (request.isauthenticated) { using (html.beginform("logoff", "account", formmethod.post, new { id = "logoutform", @class = "navbar-right" })) { @html.antiforgerytoken() <ul class="nav navbar-nav navbar-right"> <li> @html.actionlink("hello " + user.identity.getusername() + "!", "index", "manage", routevalues: null, htmlattributes: new { title = "manage" }) </li> <li><a href="javascript:document.getelementbyid('logoutform').submit()">log off</a></li> </ul> } } else { <ul class="nav navbar-nav navbar-right"> <li>@html.actionlink("register", "register", "account", routevalues: null, htmlattributes: new { id = "registerlink" })</li> <li>@html.actionlink("log in", "login", "account", routevalues: null, htmlattributes: new { id = "loginlink" })</li> </ul> }
implementing custom uservalidator
has been covered here: how can customize asp.net identity 2 username taken validation message?
rather hitting database every page request display handle, add claim during signin.
define own claim:
public static class customclaimtypes { public const string handle = "http://schemas.xmlsoap.org/ws/2014/03/mystuff/claims/handle"; }
during signin, set claim:
private async task signinasync(applicationuser user, bool ispersistent, string password = null) { authenticationmanager.signout(defaultauthenticationtypes.externalcookie); var identity = await usermanager.createidentityasync(user, defaultauthenticationtypes.applicationcookie); //get handle , add claim identity. var handle = getthehandle(); identity.addclaim(new claim(customclaimtypes.handle, handle); authenticationmanager.signin(new authenticationproperties() { ispersistent = ispersistent }, identity); }
then, via extension method can read out same way getusername()
:
public static class identityextensions { public static string gethandle(this iidentity identity) { if (identity == null) return null; return (identity claimsidentity).firstornull(customclaimtypes.handle); } internal static string firstornull(this claimsidentity identity, string claimtype) { var val = identity.findfirst(claimtype); return val == null ? null : val.value; } }
finally, in view:
@user.identity.gethandle()
Comments
Post a Comment